Im new at scripting and im trying to write a script using perl that will make sure there are 2 command line integer arguments and then check if the 2nd argument is greater than the first. i believe im close but still receive my error message even when i have 2 arguments and the second part gives me an error about an useless array
these are the errors im receiving
/export/home/sieben01/itec400/homework> ./intlist.pl 1 2
Useless use of array element in void context at ./intlist.pl line 19.
error: incorect number of arguments
usage: intlist.pl a b (where a < b)
unless ($#ARGV==2){
print "error: incorect number of arguments",
"\n",
"usage: intlist.pl a b (where a < b)",
"\n";
exit 1
}
if ($ARGV[1] => $ARGV[2]){
print "error: first argument must be less than the second asrgument",
"\n",
"Useage: intlist.pl a b (Where a < b)",
"\n";
exit 1
}
$#array gives you the index of the last element of the array. Since numbering starts at 0 (as in most computing languages), a 2 element array will return 1. An alternative would be using the scalar function.
seems easier but im doing this for a class and were supposed to use arrays.
heres what i have now. i dont understand it really. any help?
unless ($#ARGV==2){
print "error: incorect number of arguments",
"\n",
"usage: intlist.pl a b (where a < b)",
"\n";
exit 1;
}
if ($ARGV[1] => $ARGV[2]){
print "error: first argument must be less than the second argument",
"\n",
"Usage: intlist.pl a b (Where a < b)",
"\n";
exit 1;
}
else {
$COUNTER=$ARGV[1];
while($COUNTER <=$ARGV[2]){
print $COUNTER;
if ($COUNTER <= $ARGV[2]0{
print ",";
}
else {
print "\n";
its supposed to accept exactly 2 integer arguments where the first argument must be less than the second argument. The script will print a comma separated list of integers starting with the first argument up through the second argument.
#!/usr/bin/perl
use strict;
use warnings;
my @array;
if (@ARGV != 2){
print "error: incorect number of arguments",
"\n",
"usage: intlist.pl a b (where a < b)",
"\n";
exit 1;
}
if ($ARGV[0] >= $ARGV[1]){
print "error: first argument must be less than the second argument",
"\n",
"Usage: intlist.pl a b (Where a < b)",
"\n";
exit 1;
}
foreach my $argnum ($ARGV[0] .. $ARGV[1]) {
push @array, "$argnum";
push @array, ",";
}
pop(@array);
print @array;
print "\n";
---------- Post updated at 20:59 ---------- Previous update was at 20:53 ----------
Test run:
$ ./intlist.pl
error: incorect number of arguments
usage: intlist.pl a b (where a < b)
$
$ ./intlist.pl 20 10
error: first argument must be less than the second argument
Usage: intlist.pl a b (Where a < b)
$
$ ./intlist.pl 10 10
error: first argument must be less than the second argument
Usage: intlist.pl a b (Where a < b)
$
$ ./intlist.pl 10 20
10,11,12,13,14,15,16,17,18,19,20
$