Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

@ARGV / Command Line Arguments

by Anonymous Monk
on May 15, 2002 at 14:44 UTC ( #166727=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a slight problem in that my $ARGV[2] parameter isn't working from the command line. In the program below, i want $ARGV[2] to equal 250, however i want it specified from the command line, not in the script. When $ARGV[2] in the code is replaced with 250 the script works prefectly. can anyone suggest where i'm going wrong with how i've used $ARGV[2]????
#! /usr/local/bin/perl -w use strict; my $num_of_params; my $num_of_params = @ARGV; if ($num_of_params < 3) { die ("not enough params!!!"); } open (INPUT, $ARGV[0]) or die "unable to open file"; open (OUTFILE, ">$ARGV[1]"); my $count = 1; my $line; while (<>) { chomp; $line = $_; if (/>/) { ++$count; } print "\>$count\n", $line, "\n\n" if length $_ >= $ARGV[2]; print OUTFILE"\>$count\n", $line, "\n\n" if length $_ >= $ARGV[2]; } close INPUT; close OUTFILE;
thanks :-)

Replies are listed 'Best First'.
Re: confused
by davorg (Chancellor) on May 15, 2002 at 14:58 UTC

    Your problem is the line

    while (<>)

    This is wrong for two reasons.

    1. You've gone to the trouble of opening a specific input filehandle, INPUT, so you should be reading from that.
    2. When you use the diamond operator, the first thing it does is to use shift to remove the first element from @ARGV. This means that there are now only two elements left in @ARGV and $ARGV[2] is undefined.

    You should use

    while (<INPUT>)


    Look for the section on "I/O Operators" in perldoc perlop.


    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: confused
by TStanley (Canon) on May 15, 2002 at 15:10 UTC
    Personally, I would have written your code as follows: (UNTESTED !!!)
    #!/usr/local/bin/perl -w use strict; my $params=@ARGV; my ($input,$output,$length); if($params < 3){ die "Not enough parameters"; }else{ ($input,$output,$length)= @ARGV; } open(INFILE,"$input")||die "Can't open $input: $!\n"; open(OUTFILE,">$output")||die "Can't open $output: $!\n"; my $count=1; while(<INFILE>){ chomp; if(/>/){ ++$count; } if(length($_) >= $length){ print "\>$count\n",$line,"\n\n"; print OUTFILE "\>$count\n",$line,"\n\n"; } } close INFILE; close OUTFILE;

    UPDATE:Changed code per Biker's suggestion. And what is really sad, is that I actually had it right the first time, and changed it. :-(

    Never underestimate the power of very stupid people in large groups -- Anonymous

      I believe this line:

      ($input,$output,$length)= shift @ARGV;
      should read:
      ($input,$output,$length)= @ARGV;

      Everything went worng, just as foreseen.

      If it was my script, I'd write it like this:

      #! /usr/local/bin/perl -w use strict; die "Not enough params!!!\n" if @ARGV < 3; my ($input, $output, $length) = @ARGV; open INPUT, $input or die "unable to open $input: $!\n"; open OUTFILE, ">$output" or die "unable to open $output: $!\n"; my $count = 1; while (<INPUT>) { chomp; ++$count if />/; if (length >= $length) print ">$count\n$_\n\n"; print OUTFILE ">$count\n$_\n\n"; } } close INPUT; close OUTFILE;

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

Re: confused
by Biker (Priest) on May 15, 2002 at 14:57 UTC

    I guess your main problem is that you're not reading the INPUT file mentioned in $ARGV[0]. You're reading from STDIN.

    Update: davorg is correct. Anyway, s/he's not reading the file s/he thinks s/he reads.

    Everything went worng, just as foreseen.

      Actually he's not reading from STDIN. He's reading from the first file named on the command line.


      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://166727]
Approved by dsb
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2018-06-21 11:19 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.