passing multiple values into an argument as an array ?

J

Jack

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];
 
B

Brian McCauley

[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://....

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?". :)
 
D

Dave Weaver

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";
 
D

DJ Stunks

Dave said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top