!/usr/bin/perl -w -p s/^(\*+)/ $n = length($1), $n < $o ? @l[$n+1..$#l] = (0)x($#l-$n) : 0, $l[$n]++, $o = $n, join(".",@l[1..$n]) /e; #### !/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;