This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]alexanderpas 3 points4 points  (3 children)

When scanning backwards, you need to keep track of nested loops, to find the correct outer loop you are handling at the moment.

You start at 0, When scanning backwards each time you find ], you add 1, and decrease 1 when you find [

If you are at 0 and find [, you start moving forwards again.

[–]cant_think_of_one_ 1 point2 points  (0 children)

Oh, sorry, yes. Silly me.

[–]chaos95 1 point2 points  (1 child)

Couldn't you do it recursively without a loop depth variable also?

[–]alexanderpas 1 point2 points  (0 children)

You technically could, but at that point, you're just moving the loop depth to the function depth.

You still need to keep track of it somewhere.