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

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

Replies are listed 'Best First'.
Re: Re: Re: Re: help with search and match
by John M. Dlugosz (Monsignor) on Aug 26, 2002 at 21:36 UTC
    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
        while(defined(local $_=<$fh>)){chomp;

        Why do you write it that way? That is, why localize $_, and why test for defined when the line read can only be false at EOF?