#!/usr/local/bin/perl -w use strict; my $is_nested = 0; my @chunks = ("\cJ\cI ", "\cJ\cI\cI ", "\cJ\cI\cI ", "\cJ\cI ", "\cJ\cI\cI \cJ\cI BOOL is true!\cJ", "\cJ\cIBOOL is false!\cJ", ''); my $opening_tag = qr/\ <--- this tag # <--- nested open tag # <--- end tag for the nested open tag # <--- end tag for this tag (the one that has # to be picked up) # my $nested = 0; # count of nested open tags found. # SECOND WHILE while ((($chunks[++$found_i] =~ m/^$closing_tag/) ? ($nested > 0 ? $nested-- : 0) : ($chunks[$found_i] =~ m/^$opening_tag/ ? ++$nested : 1)) && $found_i < @chunks) { print "F: $found_i ", "N: $nested ", "C: $chunks[$found_i]", ; } }