in reply to Lottery Numbers
At least your intuition is accurate, wrt the inefficiency of the code. Not that it's all that bad, and for a script like this, who cares if it runs in 3 seconds or 6?
Here's some suggestions:
while (<FILE>) { # line at a time my( $game,$month,$day,$year,$one,$two,$three,$four,$five,$bonus) = s +plit; for ( $one,$two,$three,$four,$five ) { $hash{$_}++; } $bonus{$bonus}++; # bonus }
Let perl do the grunt work.push @draw, ($key) x $hash{$key};
since, I'm assuming, a number can only appear in the 'bonus' column if it also appears at least once in one of the other columns.foreach my $key ( keys %hash ) { push @draw, ($key) x $hash{$key}; push @mega, ($key) x $hash{$key} if exists $bonus{$key}; }
Here's a solution using an aux hash:my $number; do { $number = draw_regular(); } while ( grep { $_ == $number } @picked ); push @picked, $number;
my $number; do { $number = draw_regular(); } while ( exists $seen{$number} ); $seen{$number}++; push @picked, $number;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Lottery Numbers
by OverlordQ (Hermit) on May 05, 2004 at 00:20 UTC | |
by Anonymous Monk on Jun 02, 2015 at 19:31 UTC |