in reply to Hash w/ multiple values + merging
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..>#!/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"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Hash w/ multiple values + merging
by sophix (Sexton) on Feb 07, 2010 at 22:21 UTC | |
by planetscape (Chancellor) on Feb 07, 2010 at 23:08 UTC | |
by sophix (Sexton) on Feb 07, 2010 at 23:30 UTC | |
by Corion (Patriarch) on Feb 07, 2010 at 22:27 UTC | |
by sophix (Sexton) on Feb 07, 2010 at 22:46 UTC | |
by biohisham (Priest) on Feb 07, 2010 at 22:56 UTC | |
by sophix (Sexton) on Feb 07, 2010 at 23:04 UTC | |
by Anonymous Monk on Feb 07, 2010 at 23:11 UTC |