HalNineThousand has asked for the wisdom of the Perl Monks concerning the following question:
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:
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.%setting = map {/^--(.*?)=/ => /=(.*)$/} @ARGV; %file = map {$_ => (s/^([^=-]*)$/$1/)} @ARGV; %switch = map {$_ => (s/^--([^=]*)$/$1/)} @ARGV;
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parsing @ARGV w/ Map Function
by ELISHEVA (Prior) on Feb 14, 2011 at 07:18 UTC | |
by HalNineThousand (Beadle) on Feb 14, 2011 at 07:59 UTC | |
|
Re: Parsing @ARGV w/ Map Function
by wind (Priest) on Feb 14, 2011 at 06:01 UTC | |
by eff_i_g (Curate) on Feb 14, 2011 at 16:48 UTC | |
|
Re: Parsing @ARGV w/ Map Function
by jwkrahn (Abbot) on Feb 14, 2011 at 06:46 UTC | |
|
Re: Parsing @ARGV w/ Map Function
by Anonymous Monk on Feb 14, 2011 at 06:31 UTC |