perl syntax help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl syntax help
# 1  
Old 05-26-2010
perl syntax help

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)


Code:
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
}

# 2  
Old 05-26-2010
$#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.
Code:
$ cat args.pl
print '      $#ARGV: ', $#ARGV, "\n";
print 'scalar @ARGV: ', scalar @ARGV, "\n";
$ perl args.pl 1 2
      $#ARGV: 1
scalar @ARGV: 2
$ perl args.pl 1 2 3
      $#ARGV: 2
scalar @ARGV: 3

# 3  
Old 05-28-2010
seems easier but im doing this for a class and were supposed to use arrays.

heres what i have now. Smilie i dont understand it really. any help?

Code:
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";

# 4  
Old 05-28-2010
What should this script do if a < b ?
# 5  
Old 05-28-2010
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.
# 6  
Old 05-28-2010
Code:
#!/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:
Code:
$ ./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
$

# 7  
Old 05-29-2010
thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

A Perl Syntax Question.

Greetings! Here's what I believe is a "simple one" for the community tonight ;) What I'm trying to do is assign a "true/false" value to a variable depending upon whether a named process (some-process) exists; and then test for this value in the succeeding logic. I banged my head against the... (2 Replies)
Discussion started by: LinQ
2 Replies

2. UNIX for Dummies Questions & Answers

Perl syntax

Query with perl syntax Aim: is to change a perl script to use a new file I was required to replace - entries \"$entries\" with -lib <full_path_to_filename> So in the code detector.pm sub rundetector { my $class = shift; mkdir($resultDirectory); my... (3 Replies)
Discussion started by: sa@@
3 Replies

3. Shell Programming and Scripting

Perl syntax

I'm a newbie to perl scripting. Can someone please explain the syntax below. Specifically what does -> and => do? $tz->site( => $site); (10 Replies)
Discussion started by: scj2012
10 Replies

4. Programming

Perl syntax question

Hallo everybody, I have a following problem - I'm doing a map funciont to fill in a HTML table and I want to use some radiobutton groups. Unfortunatelly, they are grouped by names, so I have to add some "counter" that will divide one row from another, and I'm using CGI.pm for generating the... (3 Replies)
Discussion started by: duskos
3 Replies

5. Programming

Syntax error in perl program.

Hi, i am running this code but i am getting syntax error #!/usr/bin/perl use warnings; use strict; use XML::LibXML; use XML::LibXML::Reader; use Data::Dumper; my $file; open( $file, 'DTC_Specification_transformed.xml'); my $reader = XML::LibXML::Reader->new( IO => $file ) or die... (1 Reply)
Discussion started by: veerubiji
1 Replies

6. Shell Programming and Scripting

Perl syntax that I don't understand.

I'm just trying to confirm that I understand someone's code correctly. If someone has code that says: $foo ||= mysub(); I'm assuming that it means if $foo is nothing or undef, then assign it some value via mysub(). If I'm wrong on this, please let me know. Also, what's the difference... (4 Replies)
Discussion started by: mrwatkin
4 Replies

7. Shell Programming and Scripting

PERL Syntax Errors

Hi, I am a newbie to PERL and working on a script. When running it I get a lot of compilation errors. The actual command in the program (which is within a case structure) is given below # This gives the actual count of inquires from a log file (It works fine when I type this on the... (2 Replies)
Discussion started by: nurani
2 Replies

8. Shell Programming and Scripting

perl version for syntax errors

All, Does it matter what perl verios your running when you get syntax errors? on version 5.6.1 the code works fine, but on 5.8.0 the code gets errors? #!/usr/bin/perl #use strict; #use warnings; my $mess = 'messages'; my $mess1 = 'messages.1'; my $mess2 = 'messages.2'; my... (13 Replies)
Discussion started by: bigben1220
13 Replies

9. UNIX for Advanced & Expert Users

perl explain syntax !!!

hi all i was going through some perl code i came across this line and i am not getting what is exactly going on .. $$this{localtion} = GetName->GetVarName("EXE_DIR") ; what is the red part doing in above code (2 Replies)
Discussion started by: zedex
2 Replies

10. Shell Programming and Scripting

Perl command syntax for C:\dir

Hi Everyone, Perl command syntax that would display my ... C:\dir .... Regards, asabzevari (1 Reply)
Discussion started by: asabzevari
1 Replies
Login or Register to Ask a Question