in reply to Merging two data sets
Think in the following order,
- Description of problem
- Data structures to hold data
- Relevant operations on those data structures to perform the desired operation/processing
#!/usr/bin/perl -w use strict; my %file1hash; my ($fin1, $fin2) = qw|alpha.dat beta.dat|; open (DAT1,"$fin1"); while(my $line=<DAT1>) { chomp($line); next if $line=~m/^id/; my ($id,$x1,$x2)=split(' ' ,$line); push @{$file1hash{$id}}, "$x1 $x2"; } close DAT1; open (DAT2,"$fin2"); while (my $line=<DAT2>) { chomp($line); next if $line=~m/^id/; my ($id,$word)=split(' ', $line); if (exists $file1hash{$id}) { for my $x (@{$file1hash{$id}}){ print qq|$id\t$x\t$word\n|; } } } close DAT2;
Makes..
A 12 45 hello A 10 12 hello A 15 74 hello B 23 15 goodbye D 19 10 happy D 11 12 happy E 14 5 black E 34 31 black C 11 41 blue C 18 12 blue
Analysis: Your code was creating a hash with simple elements when what you really wanted was a hash of array references (incorrect choice of data structure). When you have > 1 element you can iterate over them and get them all.
Celebrate Intellectual Diversity
|
---|
In Section
Seekers of Perl Wisdom