Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Testing for the presence of a hash key in a filename

by suaveant (Parson)
on May 25, 2007 at 14:51 UTC ( [id://617506]=note: print w/replies, xml ) Need Help??


in reply to Testing for the presence of a hash key in a filename

my %list_of_keys = qw(DNI 1 SNI 1 JRN 1); my $filename = 'foo,JRN,SNI,bar'; my $regex = join('|', map { "\Q$_\E" } keys(%list_of_keys)); $regex = qr{\b($regex)\b}; # case insensitive? while($filename =~ /$regex/ig) { my $key = $1; print "Found key $key\n"; }
Also look at Regex::PreSuf which builds a better regex from a list of words.

Of course... if you always have a set delimeter you can do something like

my @values = split ',', $filename; for(@values) { print "Found key $_\n" if $list_of_keys{$_}; }
That is a better way to do it if you have something you can split on reliably

                - Ant
                - Some of my best work - (1 2 3)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://617506]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found