From kellert at ohsu.edu Thu Jan 11 15:35:10 2001 From: kellert at ohsu.edu (Tom Keller) Date: Wed Aug 4 00:05:15 2004 Subject: creating variables on the fly (newbie question) Message-ID: I would like to create a new array for each grouping of numbers within a simple file: The file looks like this: item 1 0.1 0.12. 0.14 0.16 0.1 item 2 0.13 0.14. 0.1 0.15 item 3 0.04 0.03 0.05 0.04 -------------------- So I want @item1 = (0.1, 0.12, 0.14, 0.16, 0.1 @item2=(0.13, 0.14, 0.1, 0.15) etc. This seems so simple, but I'm stuck. Note the number of items and the number of values for each item are unknown. Thanks, Tom Keller Thomas J. Keller, Ph.D. Oregon Health Sciences University MMI Core Facility 503-494-2442 kellert@ohsu.edu http://www.ohsu.edu/core TIMTOWTDI From jkeroes at eli.net Thu Jan 11 17:57:33 2001 From: jkeroes at eli.net (Joshua Keroes) Date: Wed Aug 4 00:05:15 2004 Subject: creating variables on the fly (newbie question) In-Reply-To: ; from kellert@ohsu.edu on Thu, Jan 11, 2001 at 01:35:10PM -0800 References: Message-ID: <20010111155733.A6266@surly.eli.net> ++ 11/01/01 13:35 -0800 - Tom Keller: > I would like to create a new array for each grouping of numbers within a simple file: > The file looks like this: > [snip] > So I want @item1 = (0.1, 0.12, 0.14, 0.16, 0.1 @item2=(0.13, 0.14, 0.1, 0.15) etc. > Note the number of items and the number of values for each item are unknown. I would use an AoA (see perldoc perllol) @AoA = ( [ .1, .12, .14, .16, .1 ], [ .13, .14, .1, .15], ...etc... ); Once this is generated in memory, you can dump it to your simple file using Data::Dumper. Reloading is trivial, just "do $file" and a little error checking. (perldoc -f do) While you could create variables with differing names as in your example above, this is actually a bad idea. The reasons are illustrated at: http://www.plover.com/~mjd/perl/varvarname.html http://www.plover.com/~mjd/perl/varvarname2.html http://www.plover.com/~mjd/perl/varvarname3.html +--------------------------------------------------------------------+ | Joshua Keroes, Sr. Software Engineer, Electric Lightwave, Inc | +--------------------------------------------------------------------+ "To be creative, it's not necessary that you're seeing a new thing, but that you're looking at the same things in a new way." Dieter Gruen, ultrananocrystalline diamond film inventor TIMTOWTDI From cp at onsitetech.com Thu Jan 11 18:45:37 2001 From: cp at onsitetech.com (Curtis Poe) Date: Wed Aug 4 00:05:15 2004 Subject: creating variables on the fly (newbie question) Message-ID: How you populate variables depends greatly upon how you intend to use them. Here's one example that reads your file into a hash of arrays. An array of arrays might be more appropriate, depending upon what you want to do with the data. _______________________________ use strict; use warnings; my %input_data; my $file = "somedata.txt"; my $item; open DATA, "<$file" or die "Can't open $file: $!"; while () { chomp; if ( /^item/ ) { $item = $_; $input_data{ $item } = []; # Create an array ref next; } # Note that the regex here is just a rough hack. Some # of your data has a period at the end and I'm # assuming that you don't want that. push @ { $input_data{ $item } }, $1 if /^(0\.\d+)/; } close DATA; _______________________________ Then, to access the second element of 'item 1', you would use something like this: $input_data{ 'item 1' }->[1]; Of course, this is just a rough guide and may not be suitable for your needs. Also, note that if more than one process is likely to use that file at a time, you should use 'flock'. Cheers, Curtis -----Original Message----- From: owner-pdx-pm-list@pm.org [mailto:owner-pdx-pm-list@pm.org]On Behalf Of Tom Keller Sent: Thursday, January 11, 2001 1:35 PM To: pdx-pm-list@pm.org Subject: creating variables on the fly (newbie question) I would like to create a new array for each grouping of numbers within a simple file: The file looks like this: item 1 0.1 0.12. 0.14 0.16 0.1 item 2 0.13 0.14. 0.1 0.15 item 3 0.04 0.03 0.05 0.04 -------------------- So I want @item1 = (0.1, 0.12, 0.14, 0.16, 0.1 @item2=(0.13, 0.14, 0.1, 0.15) etc. This seems so simple, but I'm stuck. Note the number of items and the number of values for each item are unknown. Thanks, Tom Keller Thomas J. Keller, Ph.D. Oregon Health Sciences University MMI Core Facility 503-494-2442 kellert@ohsu.edu http://www.ohsu.edu/core TIMTOWTDI TIMTOWTDI From jkeroes at eli.net Thu Jan 11 19:17:39 2001 From: jkeroes at eli.net (Joshua Keroes) Date: Wed Aug 4 00:05:15 2004 Subject: creating variables on the fly (newbie question) In-Reply-To: <20010111155733.A6266@surly.eli.net>; from jkeroes@eli.net on Thu, Jan 11, 2001 at 03:57:33PM -0800 Message-ID: <20010111171738.C6266@surly.eli.net> I misread the first question. Here's one way you could parse that file and gen [and print] that AoA. I've used $/ to read the filehandle F in paragraphs. Next, we turn each paragraph into a subarray with one element/line: __BEGIN__ #!/usr/bin/perl -w use strict; use Data::Dumper; my $AoA_ref = parse_file( "your_simple_file" ); print Data::Dumper->Dump( [$AoA_ref], ['AoA_ref'] ); sub parse_file { my $file = shift or die "Usage: parse_file( )"; my @AoA; local $/ = "\n\n"; # $INPUT_RECORD_SEPARATOR open F, "< $file" or die "Can't open '$file': $!"; for ( ) { push @AoA, [split /\n/] } close F or warn; return [@AoA]; } __END__ +--------------------------------------------------------------------+ | Joshua Keroes, Sr. Software Engineer, Electric Lightwave, Inc | +--------------------------------------------------------------------+ "To be creative, it's not necessary that you're seeing a new thing, but that you're looking at the same things in a new way." Dieter Gruen, ultrananocrystalline diamond film inventor TIMTOWTDI