Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Matching nested parens

by perlfan (Vicar)
on Oct 06, 2020 at 16:50 UTC ( [id://11122571]=note: print w/replies, xml ) Need Help??


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.

Replies are listed 'Best First'.
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).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11122571]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-25 19:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found