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)]; }