my $string = '1,3,5,7,9,1,3,4,5,6,2,1,5,6,7,9'; for my $num (1..9) { my $count = () = ",$string," =~ /,$num(?=,)/g; print "$num\t$count\n"; }