Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Help Perl Program - I have compilation errors don't know where

by tangieb01 (Novice)
on May 19, 2010 at 21:26 UTC ( [id://840799]=perlquestion: print w/replies, xml ) Need Help??

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

$valid="false"; While ($valid eq "false"){ #This is the 1st Line #User didn't enter anything. print "Please enter the number of tickets you want."; chomp($NumberOfTickets = <STDIN>); if ($NumberOfTickets ne ""){ $valid = "true"; }else{ print "\nYou did not enter anything, enter a number."; Next; } #User didn't enter a digit if ($NumberOfTickets =~ /[^0-9]/){ $valid = "false"; print "\nYou should only enter a digit, enter a digit."; Next; }else{ $valid="true"; } #Number of tickets should be between 1 and 10 if ($NumberOfTickets > 10 || $NumberOfTickets < 1){ $valid="false"; print "\nYou can only have 1 to 10 tickets, reenter number."; Next; }else{ $valid="true"; } #This is the 2nd Line #You can only have 6 numbers or less on a ticket. print "Please enter how many numbers you want on each ticket."; chomp($NumbersOnTickets = <STDIN>); if ($NumbersOnTickets <= "6"){ $valid = "true"; }else{ print "\nYou can only have 1 - 6 tickets, reenter number."; chomp($NumbersOnTickets = <STDIN>); Next; } if ($NumberOnTickets ne ""){ $valid = "true"; }else{ print "\nYou did not enter anything, enter a number between 1 - 6."; Next; } #User didn't enter a digit if ($NumberOnTickets =~ /[^0-9]/){ $valid = "false"; print "\nYou should only enter a digit, enter a digit."; Next; }else{ $valid="true"; } #Number of tickets should be between 1 and 6 if ($NumberOnTickets > 6 || $NumberOfTickets < 1){ $valid="false"; print "\nYou can only have 1 to 6 tickets, reenter number."; Next; }else{ $valid="true"; } #This is the 3rd Line #Highest number must be less than or equal to 56 print "Enter the highest number you want on your tickets."; chomp($HighestNumberOnTicket = <STDIN>); if ($HighestNumberOnTicket <= "56"){ $valid = "true"; }else{ print "\nYour highest number can between 1 and 56, reenter number."; chomp($HighestNumberOnTicket = <STDIN>); Next; } if ($HighestNumberOnTicket ne ""){ $valid = "true"; }else{ print "\nYou did not enter anything, enter a number between 1 - 56."; Next; } #User didn't enter a digit if ($HighestNumberOnTicket =~ /[^0-9]/){ $valid = "false"; print "\nYou should only enter a digit, enter a digit."; Next; }else{ $valid="true"; } #Number of tickets should be between 1 and 56 if ($HighestNumberOnTicket > 56 || $NumberOfTickets < 1){ $valid="false"; print "\nYour hightest number can only be between 1 to 56, reenter num +ber."; Next; }else{ $valid="true"; } for ($i=0; $i<$NumbersOnTickets; $i++) { $lotto[$i]= int(rand($HighestNumberOnTicket)) + 1; } print "Your lottery ticket number is @lotto\n"; } }

Replies are listed 'Best First'.
Re: Help Perl Program - I have compilation errors don't know where
by MidLifeXis (Monsignor) on May 19, 2010 at 21:35 UTC

    Just a couple of small points that may help:

    • 'While' ne 'while', and 'Next' ne 'next'
    • Try indenting your code to make it easier to read.

    --MidLifeXis

      Thanks...that was my problem I had "While" when I should of had "while". Appreciate it. It works now!

        If you are using Perl 5.10 maybe you want to rewrite your code to use a switch statement given/when for a better readability. It also helps you to extend the number of tests without adding more and more if statements. See: Switch-statements
Re: Help Perl Program - I have compilation errors don't know where
by afoken (Chancellor) on May 20, 2010 at 04:43 UTC

    Another small note: Don't abuse the strings "false" and "true" as replacement for boolean values. It makes your code harder to maintain, and it is more work for perl. Worst of all, Perl consideres BOTH values as true. There are exactly three values that Perl treats as false: 0, '', and undef, and you really should use one of those, preferably the first or the second, instead of "false". All other values are treated as true, but except in rare cases, you should use 1.

    So: replace "false" with 0 or '', replace "true" with 1. Replace $whatever eq "true" with $whatever, replace $whatever eq "false" with !$whatever.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      While I agree completely with the spirit of the reply, the pedant in me demands that I quote the following from perlsyn:

      The number 0, the strings '0' and '', the empty list (), and undef are all false in a boolean context.

Re: Help Perl Program - I have compilation errors don't know where
by Corion (Patriarch) on May 19, 2010 at 21:29 UTC

    If you get compilation errors, perl tells you where they are. Why do you keep that information to yourself?

    Have you tried reducing your program to a program that has no such compilation errors? Then the error must be in a part that you removed. That would be a way to isolate the parts of the program where there are things that make no sense to perl, which in turn makes Perl raise compilation errors.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Help Perl Program - I have compilation errors don't know where
by nagalenoj (Friar) on May 20, 2010 at 03:31 UTC

    Add the following lines to your code if you haven't. Helps you solve your issues by yourself.

    use strict;
    use warnings;
    use diagnostics;
Re: Help Perl Program - I have compilation errors don't know where
by Old_Gray_Bear (Bishop) on May 19, 2010 at 21:42 UTC
    When I ran your code I got the following:
    syntax error at temp.txt line 7, near "){" syntax error at temp.txt line 121, near "}" Unmatched right curly bracket at temp.txt line 122, at end of line Execution of temp.txt aborted due to compilation errors.

    Nota Bene: If you add warnings and strictures, you get a much longer list of problems that need fixed before you try this out in Real Life.

    ----
    I Go Back to Sleep, Now.

    OGB

Re: Help Perl Program - I have compilation errors don't know where
by apl (Monsignor) on May 20, 2010 at 15:44 UTC
    Minor style canard... You should set $valid = 0; once at the top of a set of tests, and reset it to 1 only if you successfully pass all of the validation testing.
Re: Help Perl Program - I have compilation errors don't know where
by muba (Priest) on May 27, 2010 at 00:43 UTC

    And there's more.

    if ($HighestNumberOnTicket <= "56")

    I'm quite sure you meant <= 56 here :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://840799]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-19 12:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found