in reply to count consecutive characters
while ($str =~ /(N+)/g) { print length($1), "\n"; } [download]
See perlretut and maybe perlre for an explanation.
(Updated after reading the question more carefully this time)