Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Thanks for all the help guys - really appreciate it. I wanted to be able to add more options (-x, -y, -z) easily, and to do it I ended up using an array of hashes - @arrAllArgs. When I enter:

perl myScript -i origSamples,moreSamples -p 1,3 -o 100,200

There is now a separate statement for parsing each argument:

parseNumericArgList($args{p}, 'pipeline', 1); parseNumericArgList($args{o}, 'overlap', 1);

And the subroutine (it's long winded but I can understand it):

sub parseNumericArgList { my ($arg, $tag, $deflt) = @_; my $sing = 0; my @new = (); if (defined($arg)) { # a list was entered if ($arg =~ /,/) { my @singles = split(",", $arg); foreach $sing (@singles) { if ($sing =~ /\D/) { print "\n\nWrong value in $tag\n\n"; die $USAGE } else { if (scalar(@arrAllArgs) == 0) { push (@new, addToRef($allArgs, $tag, $sing)); } else { foreach $allArgs (@arrAllArgs) { push (@new, addToRef($allArgs, $tag, $sing +)); } } } } } else { # a single value was entered if ($arg =~ /\D/) { print "\n\nWrong value in $tag\n\n"; die $USAGE } if (scalar(@arrAllArgs) == 0) { push (@new, addToRef($allArgs, $tag, $arg)); } else { foreach $allArgs (@arrAllArgs) { push (@new, addToRef($allArgs, $tag, $arg)); } } } } else { # nothing was entered, use default if (scalar(@arrAllArgs) == 0) { push (@new, addToRef($allArgs, $tag, $deflt)); } else { foreach $allArgs (@arrAllArgs) { push (@new, addToRef($allArgs, $tag, $deflt)); } } } @arrAllArgs = (); @arrAllArgs = @new; } sub addToRef { my ( $refIn, $addNm, $addVal ) = @_; my $ref = {}; my $k = ""; foreach $k (keys %{$refIn}) { $ref->{$k} = $refIn->{$k}; } $ref->{$addNm} = $addVal; return $ref; }
This provides me with the array of hashes @arrAllArgs
0 HASH(0x9e2930) 'overlap' => 100 'pipeline' => 1 1 HASH(0x9e55f0) 'overlap' => 200 'pipeline' => 1 2 HASH(0x9e2910) 'overlap' => 100 'pipeline' => 3 3 HASH(0xd8ead0) 'overlap' => 200 'pipeline' => 3
And then when I get to the main block of code it's a simple matter of

foreach $run (@arrAllArgs) { foreach $ak (keys %{$run}) { $runArgs{$ak} = $run->{$ak}; } processOne(); }
Have to add a bit to cope with non-numeric arguments and I'm there. Hope this helps anyone else in the same sitch....

In reply to Re: Storing multiple arguments in a data structure that allows for future expansion by appleb
in thread Storing multiple arguments in a data structure that allows for future expansion by appleb

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-23 18:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found