This is really more academic than anything else, since it can be done with a loop, but I'm interested in this since I've just learned about the map function and am using it as a tool to learn more. (I'm self taught, so there are times I go back and look for obvious things I've missed.)

When I read about map, I was thinking that it might be easy to create a hash from @ARGV simply, then realized for what I want, it'd take three hashes. For my use, I have three kinds of arguments: 1) Settings (like foo=bar, usually written --foo=bar), flags or switches (like --redirect or --nooutput), and file names (given as just the file name).

I'm also wondering if I'm not clear on the range of what's possible for expressions, so I've been experimenting. I found that in three lines I could pull out all the settings and put them in one hash with a "key => value" arrangement, that I could also get a list of all the filenames (arguments without a double dash in front of them), and get a hash of all the switches, with each switch name being a key in the hash and the corresponding value being a 1.

Here's what I did:

%setting = map {/^--(.*?)=/ => /=(.*)$/} @ARGV; %file = map {$_ => (s/^([^=-]*)$/$1/)} @ARGV; %switch = map {$_ => (s/^--([^=]*)$/$1/)} @ARGV;
The first one is the only one I think works well. It pulls out the key and value for each argument in @ARGV that starts with "--" and has an equals sign, like "--foo=bar." The 2nd one had to go next since the 3rd messes up the arguments for future passes through @ARGV. In the 2nd, I'm counting on the value for the number of replacements made to provide a 0 or a number. If it's 0 and no replacements are made, then the hash value for that argument will be 0, or false.

The third works like the second, but would always have to go last, since it changes the values in @ARGV.

As I mentioned, this is academic and not something I have to have, but now I'm curious and wondering just how much could be done. For example, is there any way I could use the map function and pull out all the settings (--foo=bar) AND also pull out all the switches (--redirect) by using a regex to find a string that starts with "--" and has an "=" in it and pulls the value from after the equals, or, if there is no equals, pulls the entire string as a key and creates a 1 as a value in the hash?

In other words, it'd change "--foo=bar" into a hash key/value set like "foo => bar" but it would also convert "--redirect" to the key/value set "redirect => 1". And if it did that, then I'd like to put filenames in the hash, as well. (And if I could make each filename a key and make the value a number so the values tell the order of the filenames, that'd be great, but I don't think that's possible.)

I don't know if I'm being clear enough, I'm still getting used to the mapping and this takes me into a new area with regexes. I'm just wondering if there's enough flexibility to do all this, and do it without going through @ARGV multiple times.


In reply to Parsing @ARGV w/ Map Function by HalNineThousand

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.