in reply to help with search and match

One of the main reasons for Perl's power are it's hashes.

#!/usr/bin/perl use strict; use warnings; @ARGV >= 2 or die "2 args mandatory"; my ($base,$compare) = @ARGV; # always use smaller file as 'base': #UPDATE: this makes only sense if # the name field is unique in both files! #- thanks to [John M. Dlugosz] ## ($base,$compare) = ($compare,$base) ## if -s $base > -s $compare; open my $basefh,'<',$base or die "opening '$base' failed: $!"; my %base = (); while(<$basefh>){chomp; my ($name,@columns) = split; $base{$name} = \@columns; } close $basefh; open my $cmpfh,'<',$compare or die "opening '$compare' failed: $!"; while(<$cmpfh>){chomp; my ($name,@columns) = split; next unless exists $base{$name}; print "$_ [@{$base{$name}}]\n" if $base{$name}[0] == $columns[0]; } close $cmpfh;
--
http://fruiture.de

Replies are listed 'Best First'.
Re: Re: help with search and match
by John M. Dlugosz (Monsignor) on Aug 26, 2002 at 18:47 UTC
    You're assuming the first field is unique. In his example for file2, they were not. That was also the "shorter" one.

      true. So if order matters, $base and $compare should not be switched. Anyway the first field must be unique in the $base file then, otherwise it wouldn't make much sense, so the code is still not that wrong. Thanks.

      --
      http://fruiture.de
        I think the first field could still duplicate, because the second disambiguates.

        That gives me another idea on doing it, too: store the concatenation of the first two fields, and use that as the lookup. Then we don't need to do a second comparison.