Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Foreach loop

by marinersk (Curate)
on Sep 27, 2013 at 14:03 UTC ( #1055982=note: print w/replies, xml ) Need Help??

in reply to Foreach loop

Your need to work around whitespace is a Perl speciality. This looks like a C programmer's way to attempt the search; free your mind and embrace the Perl way:

Instead of index, use grep. The \s characters will accept any kind of whitespace, be it space character or tab, etc. This is the change I'd recommend:

OLD: $substr = " 1 "; NEW: $substr = '1'; my $regex = quotemeta $substr; OLD: if (index($key, $substr) != -1) {print "\nExist";} NEW: if (grep /\s$regex\s/, " $key ") {print "\nExist";}

Update: In order to ensure a leading or trailing '1' is captured, the grep was modified to include forced whitespace around $key.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2016-10-25 04:31 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (315 votes). Check out past polls.