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

What script do I add for my program to generate more than one ticket? Currenlty all it prints is one lotto ticket. Thanks in advance.

#!/usr/bin/perl #Program name: perlscript print"Content-type: text/html\n\n"; #The HTTP header print"<HTML><HEAD><TITLE> CGI/My First Form</TITLE></HEAD>\n"; print"<BODY BGCOLOR=Blue TEXT=White>\n"; print"Processing CGI form:<p>"; $formvalues=$ENV{QUERY_STRING}; @formvalues=split("&",$formvalues); print"$formvalues[0]\n"; print "<P>"; print"$formvalues[1]\n"; print "<P>"; print"$formvalues[2]\n"; print "<P>"; @formvalues2=split("=",$formvalues[0]); $NumberOfTickets=$formvalues2[1]; @formvalues2=split("=",$formvalues[1]); $NumbersOnTickets=$formvalues2[1]; @formvalues2=split("=",$formvalues[2]); $HighestNumberOnTicket=$formvalues2[1]; print "$NumberOfTickets"; print "$NumbersOnTickets"; print "$HighestNumberOnTicket"; for ($i=0; $i<$NumbersOnTickets; $i++) { $lotto[$i]= int(rand($HighestNumberOnTicket)) + 1; } print "Your lottery ticket number is @lotto\n"; print "</BODY></HTML>\n";
  • Comment on How to generate lotto numbers, i.e. How many tickets do you want? 3 and it prints 3..and so on...
  • Download Code

Replies are listed 'Best First'.
Re: How to generate lotto numbers, i.e. How many tickets do you want? 3 and it prints 3..and so on...
by kennethk (Abbot) on Jun 09, 2010 at 22:12 UTC
    In order to generate a series of lottery numbers, you use a for loop in the following way:

    for ($i=0; $i<$NumbersOfTickets; $i++) { $lotto[$i]= int(rand($HighestNumberOnTicket)) + 1; }

    Thinking on that, how do you suppose you might generate a series of lottery tickets?

    In order to generate a series of lottery tickets, you need only wrap your lottery ticket generation code in another for loop. Be careful to use a different iterator:

    for ($i_lottery=0; $i_lottery<$NumbersOnTickets; $i_lottery++) { for ($i=0; $i<$NumbersOnTickets; $i++) { $lotto[$i]= int(rand($HighestNumberOnTicket)) + 1; } print "Your lottery ticket number is @lotto\n"; }

    There are also a number of stylistic changes you may consider that would make your code more resilient to bugs. I'd be happy to expound upon them if you like, but what you've written is certainly adequate. Consider reading Use strict warnings and diagnostics or die and Foreach Loops in perlsyn. I also note you seem to be rolling your own CGI code: see lesson 2 in Ovid's CGI Course - Resurrected and Updated!