Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

parsing multiple values with getopt::long

by madM (Beadle)
on Apr 27, 2014 at 17:08 UTC ( [id://1084004]=perlquestion: print w/replies, xml ) Need Help??

madM has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
Im trying to parse multiple values to an array without success.. when i print the array.. i can only see the first value that i enter.. for example if i wanted to parse the numbers 1 2 and 3 --prueba2 1 2 3 then i can only see that the number 1 is in the array
any suggestions?
##!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Getopt::Long; my @array; GetOptions ( "prueba2=s" => \@array, ) or die "not working: $!\n"; print "@array\n"; exit;

Replies are listed 'Best First'.
Re: parsing multiple values with getopt::long
by toolic (Bishop) on Apr 27, 2014 at 17:28 UTC
    As described in the documentation (Options with multiple values), you should use -prueba multiple times:
    use strict; use warnings; use Data::Dumper; use Getopt::Long; my @array; GetOptions ( "prueba2=s" => \@array, ) or die "not working: $!\n"; print Dumper(\@array); exit; __END__ foo.pl -p 1 -p 2 -p 3 $VAR1 = [ '1', '2', '3' ];

Re: parsing multiple values with getopt::long
by NetWallah (Canon) on Apr 27, 2014 at 17:28 UTC
    Change your "print" statement to :
    print "@array\nExtra params: @ARGV";
    and you will see the problem.

    The correct way to pass in multiple values is:

    perl <program> --prueba2 1 -prueba2 2 --prueba2 3
    You could get the results you are trying without repeating the parameter name, if you use the "experimental" "repeat specifier" getopt feature:
    GetOptions ( "prueba2=i{3}" => \@array, ) or die "not working: $!\n";

            What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                  -Larry Wall, 1992

Re: parsing multiple values with getopt::long
by RichardK (Parson) on Apr 27, 2014 at 17:32 UTC

    The help for Getopt::Long is pretty comprehensive, the section 'Options with multiple values' explains the different ways you could do that.

Re: parsing multiple values with getopt::long
by karlgoethebier (Abbot) on Apr 28, 2014 at 11:52 UTC

    You can also use a repeat specifier:

    GetOptions ( "prueba2=s{1,}" => \@array, ) or die "not working: $!\n";

    s{1,} means at least one. So you just need to type -p 1 2 3 instead of -p 1 -p 2 -p 3 - if like to save some energy ;-)

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1084004]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-29 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found