http://qs321.pair.com?node_id=123084


in reply to Some help with a lottery picking program

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