Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   get value of variable number of arguments (http://www.velocityreviews.com/forums/t913976-get-value-of-variable-number-of-arguments.html)

Francois Massion 10-14-2010 05:15 AM

get value of variable number of arguments
 
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?

Uri Guttman 10-14-2010 05:33 AM

Re: get value of variable number of arguments
 
>>>>> "FM" == Francois Massion <massion@gmx.de> 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 ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Francois Massion 10-14-2010 05:48 AM

Re: get value of variable number of arguments
 
On 14 Okt., 07:25, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth Francois Massion <mass...@gmx.de>:> 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.

Uri Guttman 10-14-2010 06:31 AM

Re: get value of variable number of arguments
 
>>>>> "FM" == Francois Massion <massion@gmx.de> writes:

FM> On 14 Okt., 07:25, Ben Morrow <b...@morrow.me.uk> 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 ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Mart van de Wege 10-14-2010 07:07 AM

Re: get value of variable number of arguments
 
Francois Massion <massion@gmx.de> 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.

Mart van de Wege 10-14-2010 07:12 AM

Re: get value of variable number of arguments
 
Mart van de Wege <mvdwege@mail.com> 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.


All times are GMT. The time now is 08:25 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.