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

Hi all, I would really appreicate it if people would offer their expertise to help me with my programming, by letting me know what I'm doing wrong etc. In the last hour or so I have written a very quick script to choose lottery numbers (I have used it for tonight's draw, so I'll let you know if I win :) ) It is basically a command line program that will generate the random numbers and then you can email them to yourself, nothing fancy, just something I hope will help people give me some advice on where I am going wrong.

Thank you very much for any advice, code below.

#!/usr/bin/perl print "-------------------------------------------------------\n"; print "National Lottery Number Picker \n"; print "-------------------------------------------------------\n\n"; print "How many lines do you want to play?\n"; print "Lines.....:"; $lines = <STDIN>; @numbers = (1..49); srand; print "\n"; &chooseline(); $i = 1; sub chooseline { while ($i < $lines) { &genline(); push @lines, @line; $i++; if ($i eq $lines) { print "\n\n"; last; } } print "\n\n"; } sub genline { @line = ''; $e = 1; while ($e ne "7") { $index = rand @numbers; $element = $numbers[$index]; print " $element"; if ($e eq "6") { print "\n"; push @line, $element; $newline = "\n"; push @line, $newline; last; } else { push @line, $element; } $e++; } } print "(E)mail Results / (Q)uit\n\n"; print "Action....:"; $action = <STDIN>; exit if $action =~ /^Q/i; if ($action =~ /^E/i) { print "\nEmail address...:"; $email = <STDIN>; open (MAIL, "|/usr/sbin/sendmail -t"); print MAIL "To: $email\n"; print MAIL "From: Lottery Generator\n"; print MAIL "Subject: Your Lottery Numbers\n"; foreach $lin (@lines) { print MAIL "$lin "; } close (MAIL); }

Edit kudra, 2001-11-04 Changed title

Replies are listed 'Best First'.
Re: Some help
by joealba (Hermit) on Nov 04, 2001 at 01:51 UTC
    I've taken a few shortcuts for you, and added a check to be sure you don't get more than one occurrence of any number in a line.
    $| = 1; print qq( ------------------------------------------------------- National Lottery Number Picker ------------------------------------------------------- How many lines do you want to play? ); my $num_lines = <STDIN>; $num_lines =~ s/\D+//g; die "Good. Don't waste your money!\n" unless $num_lines; my @numbers = (1..49); my $output; for (1..$num_lines) { my @line = (); my %chosen = (); for (1..6) { my $element; while (!$element || $chosen{$element}) { $element = $numbers[rand @numbers]; } push @line, $element; $chosen{$element} = 1; } $output .= join "\t", sort {$a <=> $b} @line; $output .= "\n"; } print $output; # Then, do your e-mail thing. Send $output as the message
      And BTW, srand is called implicitly at your first use of rand, as long as you're using perl 5.004 or greater. perldoc srand
Re: Some help
by Anonymous Monk on Nov 04, 2001 at 02:37 UTC
    I have implemented a lot of your suggestions above, and made the code below. Its not a serious program/project but something I wrote purely for people to critique, and you have all been very helpfull - I will remember to implement these techniques in future, thanks a lot.
    #!/usr/bin/perl use strict; print qq( ------------------------------------------------------- National Lottery Number Picker ------------------------------------------------------- How many lines do you want to play? ); print "Lines.....:"; my $lines = <STDIN>; my @lines; my @line; die "Good. Don't waste your money!\n" unless $lines; my @numbers = (1..49); srand; print "\n"; &chooseline(); my $i = 1; ######################################## # Generate The Line(s) sub chooseline { while ($i < $lines) { &genline(); push @lines, @line; $i++; if ($i eq $lines) { print "\n\n"; last; } } print "\n\n"; } ######################################### # Generate the numbers sub genline { @line = ''; my $e = 1; while ($e ne "7") { my $index = rand @numbers; my $element = $numbers[$index]; print " $element"; if ($e eq "6") { print "\n"; push @line, $element; my $newline = "\n"; push @line, $newline; last; } else { push @line, $element; } $e++; } } print "(E)mail Results / (Q)uit\n\n"; print "Action....:"; my $action = <STDIN>; exit if $action =~ /^Q/i; if ($action =~ /^E/i) { print "\nEmail address...:"; my $email; $email = <STDIN>; chomp($email); die "Don't bother then, bye!" if !$email; print ("$email, is this correct? (y/n)...:"); my $dcheck = <STDIN>; exit if $dcheck =~ /^n/i; open (MAIL, "|/usr/sbin/sendmail -t"); print MAIL "To: $email\n"; print MAIL "From: Lottery Generator\n"; print MAIL "Subject: Your Lottery Numbers\n"; foreach my $lin (@lines) { print MAIL "$lin "; } close (MAIL); }
Re: Some help
by tfrayner (Curate) on Nov 04, 2001 at 01:30 UTC
    Hi,

    First off, nice bit of code. I only have a couple of points. I would change the newlines in the mailing block to this:

    print MAIL "To: $email"; print MAIL "From: Lottery Generator\n"; print MAIL "Subject: Your Lottery Numbers\n\n";
    since the <STDIN> call still has a \n and the blank line that this introduces can cause a problem for email clients. Use chomp if you want to strip that newline for clarity.

    Secondly, someone's going to tell you so it might as well be me, use strict and warnings. They're really very good :-)

    Update: As usual, when I tried applying my own advice, strict and warnings threw up a bunch of problems. Probably the most insightful thing I can say is that you maybe don't want to name three different variables (@lines, $lines and @line) quite such similar names as it leads to confusion.

    Now, use strict requires that you be a bit more precise about the scope of your variables. I think the simplest thing here would be to declare those three variables as global (disclaimer: unfortunately I don't have time to go into passing variables into and out of functions. Hopefully someone else will, because globals aren't much fun in anything larger which you write). With that change, and a quick alteration in the $i variable for clarity (and to get around a warning when invoked with the -w warnings switch), I get this as the first part of your code:

    #!/usr/bin/perl -w use strict; print "-------------------------------------------------------\n"; print "National Lottery Number Picker \n"; print "-------------------------------------------------------\n\n"; print "How many lines do you want to play?\n"; print "Lines.....:"; my @lines; my @line; my $lines = <STDIN>; my @numbers = (1..49); srand; print "\n"; my $i = 1; &chooseline(); sub chooseline { while ($i <= $lines) { &genline(); push @lines, @line; $i++; if ($i eq $lines) { print "\n\n"; last; } } print "\n\n"; }
    With a few more mys here and there (left as an exercise to the reader :-)), this will work under strict and be a cleaner bit of code.

    Have fun,

    Tim

Re: Some help
by rchiav (Deacon) on Nov 04, 2001 at 01:53 UTC
    The only thing I'd add (beside use strict and warnings) would be to check your input data. The biggest part of programming isn't making a program work, it's making it so it's "idiot proof". For example, what if someone enters "sdfgf" for the number of lines to play? You should make sure the input is a positive integer. Also, you should check to make sure a valid email address is entered. There's actually a module that will do this, but it's name escapes me at the moment. Also, it would be a good idea to show the person the email address so they can confirm that they typed what they meant.

    Other than that, nice job :).

    Rich

Re: Some help
by Anonymous Monk on Nov 04, 2001 at 01:57 UTC
    Thanks guys, that is very helpfull advice and it is very appreciated. Thanks again.