in reply to search array for item, move to next item

I'd do it the regex way:
if ($args =~ /-iL\s+(\w+)/) { print "Found -iL, the value is $1"; }

Replies are listed 'Best First'.
Re^2: search array for item, move to next item
by kathys39 (Acolyte) on Oct 17, 2008 at 15:25 UTC
    got 3 of those replies to work - thank you! i like the quick and dirty regex way....
Re^2: search array for item, move to next item
by gone2015 (Deacon) on Oct 18, 2008 at 10:01 UTC

    Or, to avoid accidents:

    if ($args =~ /(?:\A|\s)-iL(?:\s+([^\s]+)|\s*\Z)/) { print "Found -iL, the value is ", (defined($1) ? "'$1'" : 'absent'), + "\n" ; }
    which:

    • will not find -iL at the end of some word, eg Tref-iL. (Surprisingly there doesn't seem to be a cleaner way of expressing "whitespace or start of string".)
    • uses [^\s] rather than \w -- assuming less about the value of the thing after -iL. (The original fragment used split(/\ /, ...), so strictly should use \ not \s... I have my poet's licence, if you want to see it.)
    • will find a -iL even if it's the last thing in the $args, which may or may not be a good thing to trap as an error in the input.

    Sadly, this doesn't look as pretty any more :-(

    Oh. And FWIW, none of this will cope if the $arg string contains quoted elements -- something way smarter is required !