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

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.

  • Comment on Re: Re: Re: Re: help with search and match

Replies are listed 'Best First'.
Re[5]: help with search and match
by fruiture (Curate) on Aug 26, 2002 at 21:51 UTC

    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?

        That's just while(<HANDLE>) with localized $_, I prefer it, although it's not neccessary in that script.

        --
        http://fruiture.de