@lines = <>; $n=1000; $long = ''; srand; # generating one random sequence of length $n: while ($n--){ $long .= qw<( )>[int (rand()*2) ] } $m; $m = qr/\((?:|(??{$m})++)*\)/; # main thing for (@lines, $long){ undef %occ; map { ++$occ{(length)} } /$m+/g; $max = 0; $max < $_ and $max = $_ for keys %occ; print $max," ",$occ{$max} || 1,$/; } #### # Example input: )((())))(()()) ))( ()(())() ((((()((( (()())()(())()()())())()((()(()(())()()())((()(())()(()()()()))()(())()(((()())()(()((())()(())(())) # Example output: 6 2 0 1 8 1 2 1 28 1 662 1