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

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

Replies are listed 'Best First'.
Re[6]: help with search and match
by John M. Dlugosz (Monsignor) on Aug 27, 2002 at 14:37 UTC
    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