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.


In reply to Re^2: How to make a array of hashes and search them by aaron_baugher
in thread How to make a array of hashes and search them by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.