get value of variable number of arguments

F

Francois Massion

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?
 
U

Uri Guttman

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
 
F

Francois Massion

Quoth Francois Massion said:
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.
 
U

Uri Guttman

    # 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
 
M

Mart van de Wege

Francois Massion said:
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
 
M

Mart van de Wege

Mart van de Wege said:
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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top