Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Foreach loop

by marinersk (Priest)
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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2017-12-11 19:50 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (311 votes). Check out past polls.