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

I got the code to sort. But I am trying to use grep to remove the duplicate numbers.

#!/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"; print "Your lottery ticket number are"; print "<P>"; for ($i_lottery=0; $i_lottery<$NumberOfTickets; $i_lottery++) { for ($i=0; $i<$NumbersOnTickets; $i++) { $lotto[$i]= int(rand($HighestNumberOnTicket)) + 1; } ($new, @lotto) >0; $new=@sorted; @sorted = sort { $a <=> $b } @lotto; print "@sorted"; print "<P>"; } print "</BODY></HTML>\n";
  • Comment on How do I get my code to not repeat numbers and also sort the numbers
  • Download Code

Replies are listed 'Best First'.
Re: How do I get my code to not repeat numbers and also sort the numbers
by kejohm (Hermit) on Jun 10, 2010 at 00:49 UTC

    As a side note, might I suggest using CGI.pm or a similar module for processing web forms and outputting HTML.

    Here is your sample code rewritten using CGI.pm:

    #!perl use strict; use warnings; use CGI qw(:standard); my $NumberOfTickets = param('NumberOfTickets'); my $NumbersOnTickets = param('NumbersOnTickets'); my $HighestNumberOnTicket = param('HighestNumberOnTicket'); print header(), #The HTTP header start_html( -title => 'CGI/My First Form', -bgcolor => 'Blue', -text => 'white', ), p('Processing CGI form:'), p($NumberOfTickets), p($NumbersOnTickets), p($HighestNumberOnTicket); my @lotto; for(my $i_lotto = 0; $i_lotto < $NumberOfTickets; $i_lotto++){ for(my $i = 0; $i < $NumbersOnTickets; $i++){ $lotto[$i] = int(rand($HighestNumberOnTicket)) + 1; } print p("Your lottery ticket number is @lotto\n"); } print end_html(); __END__

    CGI.pm does all of the parameter parsing for you and provides for much neater looking code. Relying on the order of the params being passed to your script is probably not the best idea.

Re: How do I get my code to not repeat numbers and also sort the numbers
by kennethk (Abbot) on Jun 09, 2010 at 23:00 UTC
    First, it's considered good form once you've started asking questions on a piece of code to continue in the same thread, or at least reference the original thread to help monks keep track. In this case, you'd link to How to generate lotto numbers, i.e. How many tickets do you want? 3 and it prints 3..and so on... with [id://843936] -- see Markup in the Monastery.

    Since you need something closer to a grab bag, I would suggest you use a list of acceptable numbers. A great utility for mixing them is shuffle in List::Util. The following sample code populates an array with numbers between 1 and 30, mixes the bag, grabs 5 values, and then prints them:

    use strict; use warnings; use List::Util qw(shuffle); my @bag = (1 .. 30); # Populate list using the range operator @bag = shuffle(@bag); # Shuffle the bag and store the results my @values = @bag[0..4]; # Get the first 5 results with an array slice print "Your numbers are @values\n";

      I was able to get the code to sort. But I am trying to use grep to take out the repeating numbers. How do I do that? This is what I have below.

      for ($i_lottery=0; $i_lottery<$NumberOfTickets; $i_lottery++) { for ($i=0; $i<$NumbersOnTickets; $i++) { $lotto[$i]= int(rand($HighestNumberOnTicket)) + 1; } grep($NumbersOnTickets,@lotto); $NumbersOnTickets=@lotto; @sorted = sort { $a <=> $b } @lotto; print "@sorted"; print "<P>"; }
        Won't filtering out duplicates leave you with too few numbers? If that's what you want, here you go:
        my %seen; my @unique = grep !$seen{$_}++, @numbers;

        This is in perlfaq4

      Thanks...sorry. I didn't know how to link.

        Don't worry about it. We don't expect you not to make mistakes - we simply expect you to learn from them.
Re: How do I get my code to not repeat numbers and also sort the numbers
by ikegami (Patriarch) on Jun 09, 2010 at 23:42 UTC