in reply to Perl Code Runs Extremely Slow

Another approach would be to use a hash for first file and use exists operator to check for the existance of each key in second. Example :
#!/usr/bin/perl -w use strict; my %fets; print "\n\n"; open IHF, "<File1" || die "Could not open File1".$!; while( <IHF> ) { my ($k, $v) = split; $fets{$k} = $v if defined ($k) and defined ($v); } close IHF ; open JHF, "<File2" || die "Could not open File2".$!; while(<JHF>) { my ($key, $val) = split; print $key." ".$fets{$key}." ".$val."\n" if exists $fets{$key} ; } close JHF ;
Thanks..

Replies are listed 'Best First'.
Re^2: Perl Code Runs Extremely Slow
by grinder (Bishop) on Jun 14, 2006 at 06:36 UTC

    A few minor problems with your code. Here's my idea of a better version (update: starting from the second verse).

    open IHF, "<", "File1" or die "Could not open File1: $!"; while( <IHF> ) { chomp; my ($k, $v) = split; $fets{$k} = $v if defined ($k) and defined ($v); } close IHF; open JHF, "<", "File2" or die "Could not open File2: $!"; while(<JHF>) { chomp; my ($key, $val) = split; print "$key $fets{$key} $val\n" if exists $fets{$key}; } close JHF;

    • another intruder with the mooring in the heart of the Perl

      I see you chomp the lines. I missed it probably because the orginal post does not seem to do it.
      Many thanks for the correction.
        Yes but you don't have to chomp the lines because split removes all whitespace, including any terminating newlines.
Re^2: Perl Code Runs Extremely Slow
by samtregar (Abbot) on Jun 14, 2006 at 18:28 UTC
    That's unlikely to work very well. File 1 is gigantic, so reading it all into a single a hash is likely to cause either an out-of-memory error or at least run into swap and run really slowly. There's a good summary of how much memory a hash will use in Devel::Size - it's likely to be much more than the size of the file which just about guarantees that it can't fit in memory in this case.

    -sam