get value of variable number of arguments

Discussion in 'Perl Misc' started by Francois Massion, Oct 14, 2010.

  1. 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?
     
    Francois Massion, Oct 14, 2010
    #1
    1. Advertising

  2. Francois Massion

    Uri Guttman Guest

    >>>>> "FM" == Francois Massion <> 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.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Oct 14, 2010
    #2
    1. Advertising

  3. On 14 Okt., 07:25, Ben Morrow <> wrote:
    > Quoth 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
    >
    >                     ^^^^^^^^
    > 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.
     
    Francois Massion, Oct 14, 2010
    #3
  4. Francois Massion

    Uri Guttman Guest

    >>>>> "FM" == Francois Massion <> writes:

    FM> On 14 Okt., 07:25, Ben Morrow <> 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 ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Oct 14, 2010
    #4
  5. Francois Massion <> 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, Oct 14, 2010
    #5
  6. Mart van de Wege <> 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.
     
    Mart van de Wege, Oct 14, 2010
    #6
    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. Alex
    Replies:
    1
    Views:
    380
    Ron Natalie
    Oct 16, 2003
  2. markus_e
    Replies:
    4
    Views:
    511
    Andre Kostur
    Apr 7, 2004
  3. Replies:
    3
    Views:
    851
  4. Navaneeth
    Replies:
    4
    Views:
    571
    Kenny McCormack
    Nov 20, 2010
  5. oldyork90
    Replies:
    10
    Views:
    360
    Jorge
    Sep 27, 2008
Loading...

Share This Page