SPUG: Lottery Analyzer

Chris Wilkes cwilkes-spug at ladro.com
Tue Feb 19 00:00:50 CST 2002

On Tue, Feb 19, 2002 at 03:19:10AM +0000, Michael wrote:
> I want to start by finding the most often occurring numbers, but I'm
> pretty new to programming, and don't really know how to go about doing it.
> I suspect I'll need to use a hash or two, but I don't know where to start.
> After finding the most often occurring numbers, I'll probably want to look
> into the numbers between the numbers.  Finding things like the average
> span between numbers; stuff like that.
> Can any of you offer advice on the data structures I'll need to work with?
> If I win, I'll give SPUG a million :)  I'd really only be able to handle
> 1-2 million by myself.

I would use a hash, making the lotto number the key and the number of
times it has shown up as the value.

That would mean you'll have a hash that looks like
	$seen{'35'} => 3 # the number 35 shows up three times

There's an example if you type "perldoc -q word"  Since you used the
split function in your example I'll use it in this modification.

The sort part is a little tricky to newcomers, do a "perldoc -q sort" to
see what's going on.  In a nutshell you're sorting by the value, and you
have to do this numerically with the <=> operator.

As for your next part on doing statistical analysis, I would check out
the Statistics:: modules on CPAN first to see if someone else has
already done the work for you.  "perldoc CPAN" to see how to use it, or
type "perl -MCPAN -e shell" and then "install Statistics::Descriptive"
to get one that does simple stuff like mode.

If you want to tackle this yourself use the @ary array as that's in the
order read in, which you'll need if you want to find out the distance
between two numbers.  Of course you'll have to make sure that the input
is in some sort of dated order.

Good luck!



use strict;

my (%seen, @ary);
while (<DATA>) {
  s/^\S+\s+//; # get rid of date as you don't need it
  foreach (split) { # loop over each value on the line
    $seen{$_}++;	# $_ is the lotto number
    push @ary, $_;	# if you wanted to 
foreach (sort {$seen{$b} <=> $seen{$a} } keys %seen) {
  print "$_\t$seen{$_}\n";
Jan-16-1999  12 14 16 33 35 45
Jan-13-1999  2  25 32 35 41 45
Jan-09-1999  3  10 11 12 19 35
Jan-06-1999  19 24 32 37 41 42
Jan-02-1999  3  8  20 36 38 39

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org

More information about the spug-list mailing list