Omega -1911 wrote:

Can anyone assist with a regex to pull lottery numbers from a page?
The following is one example I have tried:

All data from the web page is pushed into an array. ( Header and
footer info is removed before being pushed into the array.) The entire
goal here is an exercise I was playing with to produce a report of the
most common winning lottery numbers. What I will need to be able to do
is place the most common 5 numbers  (before the word "powerball") into
an array then place the powerball numbers into another array. Thanks
in advance.

@liners = split /(\s\[0-9],\s)Powerball:\s[0-9]/,$data_string;

_DATA_
22, 29, 35, 46, 52, Powerball: 2, Power Play: 5
1, 31, 38, 40, 53, Powerball: 42, Power Play: 2
6, 16, 18, 29, 37, Powerball: 24, Power Play: 2

I hope you're aware that the exercise is futile!

The program below may help. All it does is put all five numbers in
each line of data into array @balls and then remove the trailing two, copying these into @power. The loop accumulates a frequency count
of each ball number into hash %balls.

The hash keys (ball numbers) are sorted by descending frequency afterwards
into @common and the first five of these values are printed.

The results are less than spectacular as the only ball to appear more than
once is 29. But this appears at the top of the list so all is well!

Is this homework?

HTH,

Rob



use strict;
use warnings;

my %balls;

while (<DATA>) {

 my @balls = /\d+/g;
 my @power = splice @balls, -2;

 print "@balls\n";
 print "@power\n";
$balls{$_}++ foreach @balls;
}

my @common = sort {$balls{$b} <=> $balls{$a}} keys %balls;

print "\nMost common:\n";
print "$_\n" foreach @common[0..5];

__DATA__
22, 29, 35, 46, 52, Powerball: 2, Power Play: 5
1, 31, 38, 40, 53, Powerball: 42, Power Play: 2
6, 16, 18, 29, 37, Powerball: 24, Power Play: 2


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to