On Wed, 3 Oct 2001, Tyler Cruickshank wrote: > I have a hash that I am storing arrays in. Each array corresponds to > one hour of a day. After the hash has been populated I need to cycle > through the "days" and sum them up. So, I have written my hash such > that each key can be looped thru via a for loop. > > My keys are simply data.hr1, data.hr2, data.hr3, where each key is > incremented via the for loop. The problem seems to be that when I use > $specSum{$name}[$i] where $name would be data.hr1, data.hr2, data.hr3 > ... it doesnt work. How can I use a variable as a key to a hash? My > code works just fine when I write $specSum{'data.hr1'}[$i]. A snippet > of my code is pasted below. Thanks for the help. > > Cheers, Tyler. > > for(local $b=0; $b<=23; $b++){
Whoah, don't use local! You should be using my instead. Also, tehre's no need to use C style loops. To access a hash by a variable, you just grab all of the keys out of it: foreach my $key (keys %specSum) { print "$key = $specSum{$key}\n"; } An array reference at each hash element can then be dereferenced as @{$specSum{$key}} To cycle through that array ref, you can do: foreach my $item (0..@{$specSum{$key}}) { print "$item\n"; } Or, if you need to use the array's index: foreach my $i(0..$#{$specSum{$key}}) { print "$specSum{$key}->[$i]\n"; } -- Brett http://www.chapelperilous.net/ ------------------------------------------------------------------------ Delay is preferable to error. -- Thomas Jefferson -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]