in reply to Matching nested parens
An alternative approach is to iterate over each charactor; when you see a ( push it on the stack array (and change some "state" variable, maybe a "depth count" which is also the size of the stack); then when you see a ), pop the top of the stack (will be a (). Based on the depth of the nesting (or size of the stack) you can do all sorts of things with the intervening characters you see.
Re^2: Matching nested parens
by jcb (Parson) on Oct 06, 2020 at 23:39 UTC
|
Note that there is no need to track stack depth independently in Perl if you use an array as your stack with the push and pop builtins: Perl will keep track of the array length, which is accessible either by using the array in scalar context or with the $# sigil. There is an important difference: scalar @array gives the number of elements in @array (0 if @array is empty, 1 if there is one element) while $#array gives the highest index in @array (-1 if @array is empty, 0 if there is one element).
| [reply] [d/l] [select] |
|