In the interest of making life easier, CPAN modules for processing of command line options include: Getopt::Std and Getopt::Long. These modules are very handy for processing command line options of the most commonly used forms.

With that out of the way, in order to make map work the way you'd like it to, you can use a pattern expression with capturing parentheses:

#!/usr/bin/perl print "\@ARGV: [@ARGV]\n"; my @my_args = map { /^-(.*)/ } @ARGV; print "\@my_args:\n"; print "$_\n" for @my_args;

map evaluates the block in list context. The pattern with capturing parantheses, when evaluated in list context, returns either the captured text, or an empty list. What this means is that new elements are added to @my_args only when the pattern matches. Notice that the argument "baz," which doesn't begin with minus, is not added to @my_args.

Example:

$ ./code.pl -foo=bar -bar baz
@ARGV: [-foo=bar -bar baz]
@my_args:
foo=bar
bar


In reply to Re: Map not giving me what I thought it would. by converter
in thread Map not giving me what I thought it would. by the_0ne

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.