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

  Hey everyone. This is just another Lotto script written out of boredom. I really need some advise as to what I'm doing wrong in my code. The script works but I need some expert tips because I don't think I'm going about some things in the right way. By the way, I don't play the lottery I just find randomness very interesting.
#!/usr/bin/perl -w ## georgia cash 4, drawings every day. ## pretty good odds, not that i ever play. # use strict; use LWP::Simple; use HTML::TableExtract; use Date::Calc qw(Today Day_of_Week Add_Delta_Days Day_of_Week_to_Text Date_to_Text Date_to_Days Decode_Date_US); my @today = Today(); my $current_dow = Day_of_Week(@today); my @lotto_dates;
my $end_date; ## calculate what days there were lotto # my @prev_day = Add_Delta_Days(@today,0); ## # they started midday drawnings... $end_date = "2/5/2001\n"; for (;;) { @prev_day = Add_Delta_Days(@prev_day,-1); my ($sat_year,$sat_month,$sat_day) = Add_Delta_Days(@prev_day,0); my $sat_date = "$sat_month\/$sat_day\/$sat_year\n"; if ($sat_date eq $end_date) { push(@lotto_dates,"$sat_month\/$sat_day\/$sat_year\:M\n"); push(@lotto_dates,"$sat_month\/$sat_day\/$sat_year\:E\n"); last; } else { push(@lotto_dates,"$sat_month\/$sat_day\/$sat_year\:M\n"); push(@lotto_dates,"$sat_month\/$sat_day\/$sat_year\:E\n"); } } ## # theres no midday drawnings past this day... $end_date = "4/6/1997\n"; for (;;) { @prev_day = Add_Delta_Days(@prev_day,-1); my ($sat_year,$sat_month,$sat_day) = Add_Delta_Days(@prev_day,0); my $sat_date = "$sat_month\/$sat_day\/$sat_year\n"; if ($sat_date eq $end_date) { push(@lotto_dates,"$sat_month\/$sat_day\/$sat_year\:E\n"); last; } else { push(@lotto_dates,"$sat_month\/$sat_day\/$sat_year\:E\n"); } } ## # there are 131 dates on one page my @html; my $count = 131; foreach my $line (@lotto_dates) { if ($count == 131) { my $url = "http://www.georgialottery.com/lottery/win.cgi?game= +7&ten=yes&enddate=$line"; my $url_data = get $url; push(@html,$url_data); $count = 0; } $count++; } ## i shouldn't do this, should i? # open(HTML,">html.txt"); print HTML @html; close(HTML); ## extract table info from array # my $extract = new HTML::TableExtract( headers => ['Date','Numbers'] ); my $ts; my $row; undef $/; ## help? # open(HTML, "html.txt"); my $lotto = <HTML>; close(HTML); open(NUMBERS, ">c4_numbers.txt"); $extract->parse($lotto); foreach $ts ($extract->table_states) { foreach $row ($ts->rows) { print NUMBERS join(":", @$row), "\n"; } } close(HTML); close(NUMBERS);

--JD

Replies are listed 'Best First'.
Re: Just Another Lotto Script
by ckohl1 (Hermit) on Mar 06, 2002 at 16:33 UTC

    As a side note, I am the programmer that writes the code for the maintenance of GA lottery. The upside to that is that I code it in Perl ;).

    Even some of the components on the interactive voice response units that playback the winning numbers, make data calls through Perl scripts.



    Chris
        Very interesting. I just need to get you to email me the results :). But that would take the fun out of it.

         --JD
      The question that everyone has, but nobody dares to ask: are lotteries really random? {grin}

      ++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
      Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
      -- vs lbh hfrq OFQ pnrfne ;)
          - Whreq
      

          Yes, I really wonder that. I could take it a step further... is randomness really random after all? For example: Over a period of time things could be random but when does it stop or does it? Say this lottery thing... you have zillions of past results could it be predictable then? Kind of silly but interesting.
           --JD
Re: Just Another Lotto Script
by scain (Curate) on Mar 06, 2002 at 16:10 UTC
    Could you be a little more specific than "I don't think I'm going about some things in the right way"?

    Does it not compile? Do you not get the results you exect? How are they different from what you expect? Why do you expect us to believe you don't play the lottery?

    Scott

        Scott, everything works like I want it to I'm just curious about a couple of parts ->
      ## i shouldn't do this, should i? # open(HTML,">html.txt"); print HTML @html; close(HTML); ## extract table info from array # my $extract = new HTML::TableExtract( headers => ['Date','Numbers'] ); + my $ts; my $row; undef $/; ## help? # open(HTML, "html.txt"); my $lotto = <HTML>; close(HTML);
         --JD
        Sorry, I missed your comments in the code.

        Well, of course the two short answers are "if it works..." and "TMTOWTDI". I guess neither of those are what you're looking for.

        In general, there is nothing wrong with what you did. One style point though: when you open a file, always check that it completed successfully:

        open HTML, ">html.txt" or die "couldn't open html.txt: $!\n";
        Writing it to a file is a good idea, at least in development stages, that way you know what you've got. Would you rather not write the html file? You could "stringify" @html, if no other way than with a foreach loop:
        foreach my $line (@html) { $html .= $line; }
        Seem reasonable?

        Scott