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...

You should explain what you're actually asking here — don't make people browse through your account profile to figure out which "earlier question" you're referring to.
–
WyzardOct 21 '11 at 14:23

Does this have anything to do with the linux tag, you placed?
–
Lelouch LamperougeOct 21 '11 at 14:31

Reading the title of this question I thought you wanted this: /\bI think\b/ :)
–
pilcrowOct 21 '11 at 14:32

@ Eknath lyer Sorry, I got used to adding it as I'm on a linux box, and there are sometime differences...
–
CarpenterOct 21 '11 at 14:40

The title is misleading. This is not a regex matching question. Please come up with a better description of your problem. It might help you better understand what you are doing as well.
–
Sinan ÜnürOct 21 '11 at 15:19

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.

with this:

my $count1 = grep { $i == $_ } @arr;

grep will return a list of elements where only the expression in {} evaluates to true. This is less error-prone and much more efficient than joining the entire array and using a a regex. Also note that scalar is not necessary since the variable $count1 is scalar, so perl will return the result of grep in scalar context.

You can also get rid of this line:

push(@count2, $count1 ." times for ". $i); # pushing a "number then text and a number / scalar, string, scalar" to an array.

since you are already printing the same information in your last foreach loop.

Thanks very much MisterEd, this one got it working for me... will have to think about what it's doing though, as I'm still trying to figure out why - horses for courses :) Sorry after taking another look at it, I wanted to search for and then count $i in \@count not \@arr
–
CarpenterOct 22 '11 at 11:00