passing multiple values into an argument as an array ?

Discussion in 'Perl Misc' started by Jack, Sep 23, 2006.

  1. Jack

    Jack Guest

    hi folks,

    I am reading in arguments just fine using the code below, but I want to

    be able to add a variable number of values into an argument into perl -

    I want to be able to say
    perl -f value1 value2 ..valueN -v value value2 ..valueN

    and store the values of -f in a single array, and -v also (and the
    number of passed values could vary !)

    If anyone has any tips that would be great - Thanks, Jack

    if (@ARGV[0] eq undef) {
    $value1=@ARGV[0];
    }
    if (@ARGV[1] eq undef) {
    $value2=@ARGV[1];
     
    Jack, Sep 23, 2006
    #1
    1. Advertising

  2. Jack

    Guest

    Jack wrote:
    [a multiposted question]

    Don't multipost. It's rude.

    --
    David Filmer (http://DavidFilmer.com)
     
    , Sep 23, 2006
    #2
    1. Advertising

  3. wrote:

    > [a multiposted question]
    >
    > Don't multipost. It's rude.


    Now I think you are being too terse. At least say

    Don't multipost. It's rude. See http://....

    > David Filmer (http://DavidFilmer.com)


    I went there and found a statement "There is nothing on this website,
    and there are no plans to ever have any content here" and a link to
    another page on the same website. Can I just say "huh?". :)
     
    Brian McCauley, Sep 23, 2006
    #3
  4. Jack

    Dave Weaver Guest

    On 22 Sep 2006 20:43:51 -0700, Jack <> wrote:
    > I am reading in arguments just fine using the code below, but I want to
    > be able to add a variable number of values into an argument into perl -
    >
    > I want to be able to say
    > perl -f value1 value2 ..valueN -v value value2 ..valueN
    >
    > and store the values of -f in a single array, and -v also (and the
    > number of passed values could vary !)


    I've just discovered the clever Getopt::Declare module, which can do
    just that:

    #/usr/bin/perl
    use strict;
    use warnings;
    use Getopt::Declare;

    my $args = Getopt::Declare->new(<<END);
    -f <value>... a list of values
    -v <value>... another list of values
    END

    my @f = @{ $args->{'-f'} };
    my @v = @{ $args->{'-v'} };
    print "f = @f, v = @v\n";
     
    Dave Weaver, Sep 26, 2006
    #4
  5. Jack

    DJ Stunks Guest

    Dave Weaver wrote:
    > On 22 Sep 2006 20:43:51 -0700, Jack <> wrote:
    > > I am reading in arguments just fine using the code below, but I want to
    > > be able to add a variable number of values into an argument into perl -
    > >
    > > I want to be able to say
    > > perl -f value1 value2 ..valueN -v value value2 ..valueN
    > >
    > > and store the values of -f in a single array, and -v also (and the
    > > number of passed values could vary !)

    >
    > I've just discovered the clever Getopt::Declare module, which can do
    > just that:
    >
    > #/usr/bin/perl
    > use strict;
    > use warnings;
    > use Getopt::Declare;
    >
    > my $args = Getopt::Declare->new(<<END);
    > -f <value>... a list of values
    > -v <value>... another list of values
    > END
    >
    > my @f = @{ $args->{'-f'} };
    > my @v = @{ $args->{'-v'} };
    > print "f = @f, v = @v\n";


    It's unfortunate that this topic was multiposted, because David Filmer
    has already pointed out in perl.beginners that the newest version of
    Getopt::Long (version 2.35) supports this functionality natively (ie:
    without split()-ing) though the module's author lists this
    functionality as 'experimental'.

    -jp
     
    DJ Stunks, Sep 26, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    420
    Dennis Lee Bieber
    Nov 26, 2010
  2. Tobias Blass

    multiple values for keyword argument

    Tobias Blass, Jan 29, 2011, in forum: Python
    Replies:
    17
    Views:
    2,808
    Patty
    Feb 1, 2011
  3. Trans
    Replies:
    3
    Views:
    207
    Robert Klemme
    Jan 26, 2005
  4. ±è ÁØ¿µ
    Replies:
    7
    Views:
    265
    Einar Magnús Boson
    Nov 27, 2008
  5. User1014
    Replies:
    1
    Views:
    198
    Richard Cornford
    Nov 30, 2006
Loading...

Share This Page