Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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.


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 wandering the Monastery: (8)
As of 2015-07-03 21:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (56 votes), past polls