Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Merging two data sets

by InfiniteSilence (Curate)
on Jun 13, 2014 at 20:28 UTC ( [id://1089849]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1089849]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-23 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found