in reply to Best way to match items in an array
use strict; use warnings; sub zip { my $file = shift; print "got a zip file $file\n"; } sub txt { my $file = shift; print "got a txt file $file\n"; } my @files = ("a.zip", "b.log", "c.zaaap", "d.txt"); #before you have a particular handler developed, #you can just set it to undef my %handlers = ("zip" => \&zip, "zaaap" => undef, "txt" => \&txt); my $pattern = join("|", map {"\\\.$_\$"} keys %handlers); print "pattern = $pattern\n"; foreach (grep {/$pattern$/} @files) { m/\.([^\.]*)$/; if (defined($handlers{$1})) { &{$handlers{$1}}($_); } else { print "No handler defined for $_\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Best way to match items in an array
by ibanix (Hermit) on Dec 07, 2002 at 06:06 UTC | |
by pg (Canon) on Dec 07, 2002 at 06:24 UTC | |
by ibanix (Hermit) on Dec 07, 2002 at 21:23 UTC |