Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re: Some help by tfrayner
in thread Some help with a lottery picking program by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-19 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found