!/usr/bin/perl -w -p s { ^(\*+) # look for stars at the beginning of line }{ my $n = length($1); # get number of stars @l[$n+1..$#l] = (0)x($#l-$n) if $n < $o; # @l is global $l[$n]++; $o = $n; # $o is global join(".",@l[1..$n]); # returning this }ex;