use strict; use warnings; my @sectionCount = (1, 1); while () { chomp; if (/<\/sec>/) { pop @sectionCount; ++$sectionCount [-1]; next; } next if ! /(.*?)<\/no>/; print "Section nesting error in or before $secNum\n" if $secNum ne join ".", @sectionCount[0..(@sectionCount-2)]; } #### __DATA__ 1.1 1.1.1 1.1.2 1.1.2.1 1.1.2.2 1.1.3 1.2 1.2.1 1.2.2 1.2.2.1 1.2.2.2 1.2.3 Section nesting error in or before 1.2.2.1 Section nesting error in or before 1.2.2.2 Section nesting error in or before 1.2.3