First sorry if I should have added this to my earlier question today, but I now have the below code and am having problems getting things to add up to 100…
use strict;
use warnings;
my @arr = map {int( rand(49) + 1) } ( 1..100 ); # build an array of 100 random numbers between 1 and 49
my @count2;
foreach my $i (1..49) {
my @count = join(',', @arr) =~ m/,$i,/g; # ???
my $count1 = scalar(@count); # I want this $count1 to be the number of times each of the numbers($i) was found within the string/array.
# push(@count2, $count1 ." times for ". $i); # pushing a "number then text and a number / scalar, string, scalar" to an array.
push(@count2, [$count1, $i]);
}
#sort @count2 and print the top 7
my @sorted = sort { $b->[0] <=> $a->[0] } @count2;
my $sum = 0;
foreach my $i (0..$#sorted) { # (0..6)
printf "%d times for %d\n", $sorted[$i][0], $sorted[$i][1];
$sum += $sorted[$i][0]; # try to add up/sum all numbers in the first coloum to make sure they == 100
}
print "Generated $sum random numbers.\n"; # doesn't add up to 100, I think it is because of the regex and because the first number doesn't have a "," in front of it
# seem to be always 96 or 97, 93...
1 Answer