in reply to Foreach loop
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.
|
|---|