"Multidimensional Associative Arrays" <<<--- do they exist ?
Greetings. Is there such a thing as mutlidimensional associative array ? The reason I ask this is that I intent copy the contents of a time stamped file into one big file which will be used to update fields of a database. I intend using the filename ( remember it is time stamped) and use it as a key. FILE 1 ( Filename = 23_59_54_20_08_2002) = FIELD1FIELD2FIELD3 204060 FILE 2 (Filename = 23_59_55_20_08_2002) = FIELD1FIELD2FIELD3 302045 @myFileNameArray = (23_59_54_20_08_2002, 23_59_55_20_08_2002); @myMultiDimArray = ([20, 40, 60], [30, 20, 45]); I guess what I am really asking here is can i combine the two arrays in such a way that the Filename array becomes the key whose value is mapped to the multidimensioal array ? I hope what i am asking makes sense. ICQ : 165709889
Re: "Multidimensional Associative Arrays" <<<--- do they exist ?
on Tue, 27 Aug 2002 11:20:32 GMT, [EMAIL PROTECTED] (Q) wrote: > @myFileNameArray = (23_59_54_20_08_2002, 23_59_55_20_08_2002); > @myMultiDimArray = ([20, 40, 60], [30, 20, 45]); > > I guess what I am really asking here is can i combine the two > arrays in such a way that the Filename array becomes the key whose > value is mapped to the multidimensioal array ? Do you perhaps mean: my %files = (); $files{'23_59_54_20_08_2002'} = [20, 40, 60]; $files{'23_59_55_20_08_2002'} = [30, 20, 45]; Or are there multiple lines in each file, in which case you could use $files{'23_59_54_20_08_2002'} = [[20, 40, 60], [70, 80, 90]]; You also may want to turn around your filename, so the year comes first, especially when you want to sort (e.g. to iterate from old to new). -- felix -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: "Multidimensional Associative Arrays" <<<--- do they exist ?
On Tue, Aug 27, 2002 at 01:20:32PM +0200, Q wrote: > Greetings. > > Is there such a thing as mutlidimensional associative array ? > > FILE 1 ( Filename = 23_59_54_20_08_2002) > = > > FIELD1FIELD2FIELD3 > 204060 > > > FILE 2 (Filename = 23_59_55_20_08_2002) > = > > FIELD1FIELD2FIELD3 > 302045 You already got an answer from Felix, but let me feed you a bit more information as base-knowledge: perldoc perldsc Read the "Perl Data Structures Cookbook". There you'll find a nice overview about the stuff you need. perldoc perlref goes into far more detail, so take a look at that too. You can take your approach and build a hash of hashes my $stuff = { '23_59_54_20_08_2002' => { field1 => 20, field2 => 40, field3 => 60, }, '23_59_55_20_08_2002' => { field1 => 30, field2 => 20, field3 => 45, } } my $value = $stuff->{$fname}{field2}; # gives '40' or you could create an array of hashes: my $stuff = [ { filename=> '23_59_54_20_08_2002', data=> { field1 => ... } }, { filename=> '23_59_55_20_08_2002', data=> { ... } } } my $value = $stuff->[0]{field2} # gives '40' For both methods, You could store the data in an array if the fieldnames are of no interest to you: my $stuff = { '23_59_54_20_08_2002' => [ 20, 40, 60 ], ... ] my $value = $stuff->{23_59_54_20_08_2002}[2]# guess what and my $stuff = [ { filename=> '23_59_54_20_08_2002', data=> [ 20, 40, 60 ], }, ... ]; my $value = $stuff->[0][1]; # ditto It all depends on what you want to do with the final data. Do you want to easily access single elements? That would be a candidate for hashes. Do you want to iterate over your data to calculate statistics? Smells like arrays... And always remember: When it comes to complex data structures, Data::Dumper is your friend... -- Well, then let's give that Java-Wussie a beating... (me) Michael Lamertz| +49 2234 204947 / +49 171 6900 310 Sandstr. 122 | [EMAIL PROTECTED] 50226 Frechen | http://www.lamertz.net Germany| http://www.perl-ronin.de -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: "Multidimensional Associative Arrays" <<<--- do they exist ?
$myhash {"23_59_54_20_08_2002"} = [30,20,45] ; my $x = $myhash{"23_59_54_20_08_2002"},"\n" ; for my $i (0..$#{$x}) { print $myhash{"23_59_54_20_08_2002"}->[$i]," " ; } print "\n" ; HTH. Om. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]