Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Foreach loop

by marinersk (Chaplain)
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.


Comment on Re: Foreach loop
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2014-12-21 18:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (106 votes), past polls