Page 1 of 1
how to determine the stack an object belongs to
Posted: Wed Jun 18, 2008 1:04 pm
by rozek
Hello!
I have a really silly question: how do I determine the (name of) a stack a given object belongs to?
Let's say, the object is "the target" of a message. If I look at "the long name of the target", the name of the stack is part of it. But how do I robustly extract the stack name?
At first, one could think of
set the wholeMatches to true
local TargetName; put the long name of the target into TargetName
word (WordOffset("stack",TargetName)+1) of TargetName
which works in many situations, but not all (e.g., if the object is placed on a card named ' "stack" ' (where the single quotes mark begin and end of the name), the abovementioned lines fail miserably...)
Is there a better (i.e., more direct) approach?
Thanks in advance for any help!
Posted: Wed Jun 18, 2008 1:08 pm
by rozek
By the way,
it might be really interesting to try the example I gave in my posting - for an object which is placed on a card of a substack of a mainstack.
The result was quite unexpected - for me, at least.
EDIT: well, and if you want to disturb Revolution even more, try to give the card a name which contains a single double-quote (or, perhaps, an odd number of them in general).
Posted: Wed Jun 18, 2008 1:38 pm
by trevordevore
You are pretty much left to parsing the object reference yourself. If you use the long id of an object rather than the long name then you don't need to worry about "stack" appearing in the name of a control.
Code: Select all
function StackOf pControl
local theCharNo
put the long id of pControl into pControl ## force the long id
if word 1 of pControl is not "stack" then
put offset(" of stack ", pControl) into theCharNo
if theCharNo > 0 then
delete char 1 to (theCharNo + 3) of pControl
else
put empty into pControl
end if
end if
return pControl
end StackOf
And here is a CardOf function as well:
Code: Select all
function CardOf pControl
local theCharNo
put the long id of pControl into pControl ## force the long id
if word 1 of pControl is not "card" then
put offset(" of card ", pControl) into theCharNo
if theCharNo > 0 then
delete char 1 to (theCharNo + 3) of pControl
else
put empty into pControl
end if
end if
return pControl
end CardOf
Posted: Wed Jun 18, 2008 1:44 pm
by rozek
Ah great!
I did not know that there is also a "long id" - I'll immediately try your solution!
Thanks a lot, Trevor!
Posted: Wed Jun 18, 2008 1:50 pm
by rozek
Hihi,
I just wanted to test what happes, if the name of a substack contains spaces and a single double quote...and what happened?
The "stack inspector" no longer comes up - seems that I have found a bug in the IDE
So, don't try that yourself or you'll have to rename your stack using Transcript!
Posted: Wed Jun 18, 2008 2:44 pm
by rozek
Trevor,
I just looked through your example code: in "StackOf" you explicitly check for a stack reference - but what happens, if you pass such a stack reference to "CardOf" (and choose a stack name which contains " of card ")? Shouldn't you explicitly check for stack references in "CardOf" as well (just to make your code robust)?
Posted: Wed Jun 18, 2008 2:58 pm
by trevordevore
Sure. Here is some updated code:
Code: Select all
function CardOf pControl
local theCharNo
put the long id of pControl into pControl ## force the long id
if word 1 of pControl is "stack" then
put empty into pControl -- has no card
else
if word 1 of pControl is not "card" then
put offset(" of card ", pControl) into theCharNo
if theCharNo > 0 then
delete char 1 to (theCharNo + 3) of pControl
else
put empty into pControl
end if
end if
end if
return pControl
end CardOf