IISGUY has asked for the wisdom of the Perl Monks concerning the following question:

I am relatively new to PERL. I was hoping that someone could point me in the right direction as to why the script below does not work correctly. It is supposed to take the first argument and add 1 to it until it reaches the second argument. I have tried various ways top write it - and it keeps on giving me errors. I am not looking for a direct answer - but to be pointed in the right direction
#!/usr/bin/perl $num1=$ARGV[0]; $num2=$ARGV[1]; if ( @ARGV < 2 ) { print "Number of arguments must be two \n" } elsif ( @ARGV > 2 ) { print "Number of arguments cannot be more than 2 \n" } else { ($num=$num1++); print $num; print "\n"; } unless ($num1=$num2) { print $num2; }

Replies are listed 'Best First'.
Re: Syntax trouble?
by LanX (Saint) on May 28, 2011 at 19:01 UTC
    the script doesn't do what you describe, first of all your only incrementing once:

    else { ($num=$num1++); print $num; print "\n"; }

    an then you want to loop, but without test (that should be == not =) and you are confusing until with unless (see perlsyn#Compound Statements)

    unless ($num1=$num2) { print $num2; }

    IMHO you wanted something like

    else { $num=$num1; while ( $num <= $num2 ) { print $num; $num++ } }

    Cheers Rolf

    UPDATE: corrected $num != $num1 confusion

Re: Syntax trouble?
by davies (Monsignor) on May 28, 2011 at 19:09 UTC

    I still make this mistake so frequently I'm black & blue from kicking myself. Rather than give you the answer (since that's what you ask), have a look at this, in particular the fourth paragraph ("comparison operators" being a title, not a paragraph), and look at your condition in the unless statement.

    Regards,

    John Davies

Re: Syntax trouble?
by luis.roca (Deacon) on May 28, 2011 at 19:59 UTC

    Hello IISGUY,
    A few suggestions in addition to what's been said:

    1. Check out perlsyn in the Perl documentation. You should get in the habit of adding the following to the top of your scripts:
      use strict; use warnings; use diagnostics; use Data::Dumper;
      The first two pragmas (use strict; use warnings) are *musts. The second two items *(use diagnostics; use Data::Dumper;) will give you additional information on any errors your script may be returning.

    2. Declare your variables (you will get an error if you do not add my):
      my $num1=$ARGV[0]; my $num2=$ARGV[1];
    3. Everyone has their own approach to solving a problem but why not be direct and test for the correct answer first instead of testing for incorrect input. Here's one pseudo-ish approach:
      # 1 # Ask the user for two numbers # 2 # while the first number is less than the second; # add 1 to the first number # then print out the second #^# THAT'S THE CONDITION YOU WANT #^# #3 #v# NOW DESCRIBE WHAT CAN GO WRONG AND WHAT TO DO ABOUT IT #v# # if the user enters less than two numbers # say to the user: "you didn't give me two numbers" # if the user enters more than two numbers # say to the user: "you gave me too many numbers

    I hope this helps. Good luck!

    1. If you're using Perl version 5.012 or 5.014 these are included by default
    2. Read up on Data::Dumper for more information on what it does and how to use it.

    Update 04:07:24 PM -0400 on Saturday, 5/28/11: added notes on later Perl versions and Data::Dumper.


    "...the adversities born of well-placed thoughts should be considered mercies rather than misfortunes." — Don Quixote