To associate more than one value with a hash key you need an advanced data structure, a hash of anonymous arrays, here is one way to do that, I am reading the data from two files, File1.txt and File2.txt:
#!/usr/local/bin/perl use strict; use warnings; open (FH1, "File1.txt")or die("Error opening File1 $!\n"); my %hash1; while(<FH1>){ next if /(trip|valu1|valu2)/; #skip the header my ($key, $val1, $val2)= split /\s+/; push @{$hash1{$key}}, ($val1, $val2); #hash of anonymous + array } close FH1; open(FH2, "File2.txt")or die("Error opening File1 $!\n"); my %hash2; while(<FH2>){ next if /(trip|value)/; my($key, $val3)=split /\s+/; $hash2{$key}=$val3; } close FH2; #convey common keys into one hash.. my %hash3; my ($key1, $key2); foreach $key1(keys %hash1){ foreach $key2(keys %hash2){ if ($key2 eq $key1){ push @{$hash3{$key1}}, @{$hash1{$key1}}, $hash2{$ke +y2}; } } } #Print to STDOUT print "TRIP\tvalue1\tvalue2\tvalue3\n"; foreach my $key(keys %hash3){ print "$key\t"; print "@{$hash3{$key}}\n"; }
UPDATE: Perlref and Perlreftut are additional must-reads, to be able to manipulate the data structures you need to have an idea on references and how to dereference them..>


Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

In reply to Re: Hash w/ multiple values + merging by biohisham
in thread Hash w/ multiple values + merging by sophix

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.