in reply to Re: help with search and match
in thread help with search and match

You're assuming the first field is unique. In his example for file2, they were not. That was also the "shorter" one.

Replies are listed 'Best First'.
Re: Re: Re: help with search and match
by fruiture (Curate) on Aug 26, 2002 at 18:50 UTC

    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.

        So this would be a very open method:

        #!/usr/bin/perl use strict; use warnings; my $base = shift or die; my $cmpdepth = 2; my %base = (); { open my $fh ,'<',$base or die "opening '$base' failed: $!"; while(defined(local $_=<$fh>)){chomp; my $c = \%base; for(split){ $c = ($c->{$_} = {}); } } close $fh; } while(defined(local $_ =<>)){chomp; my @s = split; my $f = 1; my $c = \%base; for( 0 .. ($cmpdepth - 1) ){ $f-- and last unless exists $c->{ $s[$_] } and $c = $c->{$s[$_]}; } print "$_\n" if $f; }

        that gives the possibility to check up to any number of fields.

        --
        http://fruiture.de