in reply to Re: How to make a array of hashes and search them
in thread How to make a array of hashes and search them
As moritz said, your @array appears to be global, so it's not getting recreated each time through your loop, and all your hash keys are getting assigned a reference to the same array. Here's how I'd rewrite your loop to eliminate that array and clarify some things:
my $j = 0; # file/column number foreach $file (@files){ open(INP,"$file") or die "Cannot open file :$!\n"; while (<INP>){ chomp; my($key,$value) = split "\t"; if(exists $trans{$key}){ $trans{$key}[$j] = $value; } } close(INP); $j++; }
When you split a line into an array and then start accessing the elements directly like $line[0], that's a good sign you might want to split it into named scalars like $key and $value instead, so you don't have to remember later what array index held what. I think it also makes it easier to see what I'm doing there when I assign the $value to the $j-th element of the array pointed to by the $key of the hash.
Aaron B.
My Woefully Neglected Blog, where I occasionally mention Perl.
|
|---|