in reply to find element in array, then advance to next element

Looks more a job for a regular expression than walking an array:
my $ports = $args =~ m/-p\s+([-\d]+)/;

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: find element in array, then advance to next element
by kathys39 (Acolyte) on Dec 09, 2008 at 19:01 UTC
    CountZero - tried this and get a return of "1" for my ports. What'd I'd really like is to grab all values (in my example above, 1-1024), then I'll split from there.

      You need list context to assign the capture(s) — note the parentheses around $ports:

      my ($ports) = $args =~ m/-p\s+([-\d]+)/;

      Otherwise (in scalar context), you'd get whether the regex matched (thus the 1).

        that's it - so sorry I missed that. Works perfectly - thanks so much!
        Of course!

        Note to self: only try to answer questions on a computer which has Perl installed so you can test what you write.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James