Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > get value of variable number of arguments

Reply
Thread Tools

get value of variable number of arguments

 
 
Francois Massion
Guest
Posts: n/a
 
      10-14-2010
I have a script with the following syntax:
extract.pl [file-to-extract] [results file] [stopword-file(s)
= 1..n]

I would like to get the value of the stopword-file(s). There may be
one or more of such file.

The following code doesn't deliver the right values:

my $source_file = shift @ARGV;
my $target_file = $ARGV[0]; # Value is OK
# one or more than one stopword file --> result NOK
my @stopwords_files = ();
foreach my $stopword_file (@ARGV) {
push @stopwords_files, $stopword_file;
}

Any suggestions?
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      10-14-2010
>>>>> "FM" == Francois Massion <(E-Mail Removed)> writes:

FM> I have a script with the following syntax:
FM> extract.pl [file-to-extract] [results file] [stopword-file(s)
FM> = 1..n]

FM> I would like to get the value of the stopword-file(s). There may be
FM> one or more of such file.

FM> The following code doesn't deliver the right values:

FM> my $source_file = shift @ARGV;

that removed the arg

FM> my $target_file = $ARGV[0]; # Value is OK

that doesn't remove the arg.
FM> # one or more than one stopword file --> result NOK
FM> my @stopwords_files = ();
FM> foreach my $stopword_file (@ARGV) {

the first value will still be the target file

also use the same names in your code as you do to describe the command
line args. why call one results file and the other target file?

FM> push @stopwords_files, $stopword_file;
FM> }

you are coding way too much!

my( $source_file, $target_file, @stopwords_files ) = @ARGV ;

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
 
 
 
Francois Massion
Guest
Posts: n/a
 
      10-14-2010
On 14 Okt., 07:25, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth Francois Massion <(E-Mail Removed)>:> I have a script with the following syntax:
> > extract.pl * *[file-to-extract] * *[results file] * *[stopword-file(s)
> > = 1..n]

>
> > I would like to get the value of the stopword-file(s). There may be
> > one or more of such file.

>
> > The following code doesn't deliver the right values:

>
> > my $source_file = shift @ARGV;

>
> * * * * * * * * * * ^^^^^^^^^^^> my $target_file = $ARGV[0]; # Value is OK
>
> * * * * * * * * * * ^^^^^^^^
> These two lines are not the same. Why not?
>
> > # one or more than one stopword file --> result NOK
> > my @stopwords_files = ();

>
> * * * * * * * * * * * ^^^^
> This does exactly nothing, so don't put it in.
>
> > foreach my $stopword_file (@ARGV) {
> > * *push @stopwords_files, $stopword_file;
> > }

>
> > Any suggestions?

>
> There are three basic approaches here:
>
> * * # using shift
>
> * * my $source_file = shift @ARGV;
> * * my $target_file = shift @ARGV;
> * * my @stopwords_files;
> * * while (@ARGV) {
> * * * * push @stopword_files, shift @ARGV;
> * * }
>
> * * # using individual assignments
>
> * * my $source_file = $ARGV[0];
> * * my $target_file = $ARGV[1];
> * * my @stopword_files = @ARGV[2..$#ARGV];
>
> * * # using one assignment
>
> * * my ($source_file, $target_file, @stopword_files) = @ARGV;
>
> I greatly prefer the last.
>
> Ben


Hi Ben,

apparently only the SHIFT approach works here. The other two
approaches create an array @stopword_files with all the file names,
not the names of the stop files only.

Thanks for your help.
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      10-14-2010
>>>>> "FM" == Francois Massion <(E-Mail Removed)> writes:

FM> On 14 Okt., 07:25, Ben Morrow <(E-Mail Removed)> wrote:
>>
>> * * # using shift
>>
>> * * my $source_file = shift @ARGV;
>> * * my $target_file = shift @ARGV;
>> * * my @stopwords_files;
>> * * while (@ARGV) {
>> * * * * push @stopword_files, shift @ARGV;
>> * * }
>>
>> * * # using individual assignments
>>
>> * * my $source_file = $ARGV[0];
>> * * my $target_file = $ARGV[1];
>> * * my @stopword_files = @ARGV[2..$#ARGV];
>>
>> * * # using one assignment
>>
>> * * my ($source_file, $target_file, @stopword_files) = @ARGV;
>>
>> I greatly prefer the last.


FM> apparently only the SHIFT approach works here. The other two
FM> approaches create an array @stopword_files with all the file names,
FM> not the names of the stop files only.

your word isn't good enough. you need to show code and data and output
and prove ben's code didn't work. all three will work if you try them
correctly. saying it didn't work is a useless statement without
information on what you did and what results you see.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
Mart van de Wege
Guest
Posts: n/a
 
      10-14-2010
Francois Massion <(E-Mail Removed)> writes:

> I have a script with the following syntax:
> extract.pl [file-to-extract] [results file] [stopword-file(s)
> = 1..n]
>
> I would like to get the value of the stopword-file(s). There may be
> one or more of such file.
>
> The following code doesn't deliver the right values:
>
> my $source_file = shift @ARGV;
> my $target_file = $ARGV[0]; # Value is OK
> # one or more than one stopword file --> result NOK
> my @stopwords_files = ();
> foreach my $stopword_file (@ARGV) {
> push @stopwords_files, $stopword_file;
> }
>
> Any suggestions?


Use Getopt::Long

Warning, following code is untested:

----- START CODE -----
#!/usr/bin/perl

use strict;
use warnings;

use Getopt::Long;

my $source_file;
my $target_file;
my @stopword_files;

my $result = GetOptions("source-file=s" => \$source_file,
"target-file=s" => \$target_file,
"stopword-files=s{,}" => \@stopword_files,
);



----- END CODE -----

After running your script with 'extract.pl --source-file <filename>
--target-file <filename> --stopword-files <one or more filenames>' your
stopword file names will be stored in the @stopword_files array.

for a working example, I have a script here that merges multiple PDFs
into one:

#!/usr/bin/perl

use Getopt::Long;
use PDF::API2;

my $target = "merged.pdf";
my @sources;
my $debug;
my $result = GetOptions("target=s" => \$target,
"source=s{,}" => \@sources,
"debug" => \$debug );
my $pdf = PDF::API2->new;
my $current_page = $pdf->openpage(-1);
for my $source (@sources) {
print "Merging file $source\n" if $debug;
my $src_pdf = PDF::API2->open($source);
my $src_pages = $src_pdf->pages;
for my $src_page (1..$src_pages) {
$pdf->importpage($src_pdf,$src_page,$current_page);
$current_page = $pdf->page(0);
}
}
$pdf->saveas($target);


Mart

--
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.
 
Reply With Quote
 
Mart van de Wege
Guest
Posts: n/a
 
      10-14-2010
Mart van de Wege <(E-Mail Removed)> writes:

> I have a script here that merges multiple PDFs
> into one:
>
> #!/usr/bin/perl
>
> use Getopt::Long;
> use PDF::API2;
>

<snip>

And of course you should use 'use strict; use warnings;' in any script
you write.

First time I forgot this in more than a year. But the script in question
runs perfectly fine with no errors or warnings after adding them.

Mart

--
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a function that expects variable arguments from a functionwith variable arguments Navaneeth C Programming 4 11-20-2010 05:35 AM
Call again a variadic function (... variable number of arguments)with same arguments that its variadic wrapper moreau.steve@gmail.com C Programming 3 12-31-2008 07:13 AM
functions and arguments.length; passing unknown number of arguments oldyork90 Javascript 10 09-27-2008 03:05 AM
Re: Calls with a Variable Number of Arguments markus_e C++ 4 04-07-2004 03:28 PM
variable number of arguments + function pointers Alex C++ 1 10-16-2003 10:31 PM



Advertisments