in reply to matching regex on an array element w/o looping the array

Here's one way:
use warnings; use strict; use File::Find; my $dir = '/full/path'; my %fullpath; my @toget = qw/foo goo moo/; #my $pattern = join "|",@words; # bad mistake on my part my $pattern = join "|",@toget; find sub { $fullpath{$_} = $File::Find::name if -f $File::Find::name; +},$dir; for (keys %fullpath) { print $fullpath{$_}; if (/($pattern)/i) { print "\t$1\n" } else {print "\tNO MATCH\n";} }

-- Regards,
Helgi Briem
helgi AT decode DOT is

Replies are listed 'Best First'.
Re: Re: matching regex on an array element w/o looping the array
by gnu@perl (Pilgrim) on Oct 03, 2002 at 18:50 UTC
    Thanks for your help, you kind of spurred me onto an idea. Here's what I did and it worked great.
    #!/usr/local/bin/perl -w use strict; use File::Find; use Data::Dumper; my $file = "/u90/gvc_archive/ama_recover/chad.lst"; my $startDir = "u90/gvc_archive/ama_recover"; my $destDir = "$startDir/4tych"; my @toget; my %sourcefiles; my @foundfiles; chdir $startDir; open(FILES,"<$file"); find(\&pushsources,"."); for (<FILES>){ s/ *$//; chomp; print "Checking $_\n"; my $file = $sourcefiles{"$_"}; print "\t $file \n" if ($file); } sub pushsources { s/\.gz$//; chomp; $sourcefiles{$_}=$File::Find::name ; }
Re: Re: matching regex on an array element w/o looping the array
by gnu@perl (Pilgrim) on Oct 03, 2002 at 17:56 UTC
    Thanks, but I'm a little confused. What is @words used for?
      I'm most awfully sorry. @words was leftover from an earlier version. I changed the name to @toget to match yours, but forgot to change it in both places. That should teach me not to fix things after cutting and pasting.

      -- Regards,
      Helgi Briem
      helgi AT decode DOT is

        Thanks, I figured it was something like that. I just thought maybe you were going to add something more that used @words. Thanks for your help though. It really did help.