For that situation, I would make a stack out of storing a the values as a list in a global script variable, e.g. $$layoutName. The last line would be the top of the stack.
Or you could make a stack out of two or more global script variables, e.g. $$layout_StackIndex and $$layoutStackValue[$$layout_StackIndex] with the latter storing each stack entry in a separate repetition.
some fundamental question:
so this $$ variable has multiple entries deliminated by a carriage return character. this seems to act like an array ?
how do I now how many lines the variable has and how do i delete the last line when i exit the layout
an where would i find this in the documentation?
Not an array but a LIST & as such, you can use any of the 'value list' functions on it:
ValueCount, LeftValues, etc.
ValueCount will return the number of values in a list.
SetVariable [ $$List ; List ( Get ( LayoutName ) ; $$List ) ]
Go to Layout [ GetValue ( $$List ; 1 ) ] //use layout name by calculation option
SetVariable [ $$List ; RightValues [ $$List ; ValueCount ( $$List ) - 1 ]