Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: concatenation of lines from two different files

by agianni (Hermit)
on Aug 17, 2007 at 14:26 UTC ( [id://633305] : note . print w/replies, xml ) Need Help??

in reply to concatenation of lines from two different files

You are reading through the second file as many times as there are lines in the first file. That's way inefficent. Instead, consider reading through the first file and putting its contents into a hash keyed off of the pertinent value. Something like (untested):
my %file1_row_for; while (my $input = <THAT_FILE>){ # only grab column 2 my ( undef, $id ) = split /;/, $input; my $file1_row_for{$id} = $input; }
Do something like that for the first file, then you can do something like this:
while ( my $input = <THAT_OTHER_FILE> ){ my ( $id2 ) = split /;/, $input; print OUTFILE $input print OUTFILE ';' . $file1_row_for{$id2} if defined $file1_row_for +{$id2} print OUTFILE "\n"; }
To generate your output. One other question, though: is there a possibility that there will be more than one matching row in the second file? Even if you think there isn't, you should probably write your code to either process that in a particular manner (output two lines?) or to die if it runs into that scenario.
perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'

Replies are listed 'Best First'.
Re^2: concatenation of lines from two different files
by steph_bow (Pilgrim) on Aug 17, 2007 at 14:31 UTC

    Dear agianni,

    Thanks a lot for your reply

    Concerning your latest remark, in cases of several matches, I intend to write the other matches next to the first match, on the same line

      If that's the case, you'll need to make the hash storing the contents of the first file a hash of arrays (or, rather, a hash of arrayrefs) rather than a simple hash, so you can store each line that matches the id:

      my %file1_row_for; while (my $input = <THAT_FILE>){ # only grab column 2 my ( undef, $id ) = split /;/, $input; # add this line to the array of data for this key push @{ $file1_row_for{$id} } = $input; }

      Then when you want to output it at the end of the line you can:

      print join ';', @{ $file_row_for{$id} } if scalar @{ $file_row_for{$id +} };

      Note that you'll just want to check for the length of the arrayref rather than definedness as in my original code.

      perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'