I am trying to run this program that I wrote and I keep getting an error message that states the following
Use of uninitialized value $ARGV[1] in substitu
Don't forget that arrays are indexed from 0 in Perl, and the elements of @ARGV
do not include the program name. You should probably be using $ARGV[0]
and $ARGV[1]
. You can check this by printing out the values of the array — index and value at the index.
See perldoc perlvar:
The array
@ARGV
contains the command-line arguments intended for the script.$#ARGV
is generally the number of arguments minus one, because$ARGV[0]
is the first argument, not the program's command name itself. See$0
for the command name.
Note that this means that if you run:
perl script.pl something or-another
the @ARGV
array has two entries: something
and or-another
in elements 0 and 1 respectively. Similarly if the script is executable:
script.pl something or-another
If you check $ARGV[2]
, you will be using the default file name with either of the invocations shown — change the 2 to a 1. Also, you should be able to move the test and assignment to $outfile
outside the loop, and only open that file just once. At the moment, you zap it for each new file, so effectively you only process the last file.