how to determine the stack an object belongs to

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
rozek
Posts: 151
Joined: Mon Jun 19, 2006 2:29 pm
Contact:

how to determine the stack an object belongs to

Post by rozek » Wed Jun 18, 2008 1:04 pm

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!
Kind regards,

Andreas Rozek

rozek
Posts: 151
Joined: Mon Jun 19, 2006 2:29 pm
Contact:

Post by rozek » Wed Jun 18, 2008 1:08 pm

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).
Last edited by rozek on Wed Jun 18, 2008 1:42 pm, edited 1 time in total.
Kind regards,

Andreas Rozek

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Contact:

Post by trevordevore » Wed Jun 18, 2008 1:38 pm

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
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

rozek
Posts: 151
Joined: Mon Jun 19, 2006 2:29 pm
Contact:

Post by rozek » Wed Jun 18, 2008 1:44 pm

Ah great!

I did not know that there is also a "long id" - I'll immediately try your solution!

Thanks a lot, Trevor!
Kind regards,

Andreas Rozek

rozek
Posts: 151
Joined: Mon Jun 19, 2006 2:29 pm
Contact:

Post by rozek » Wed Jun 18, 2008 1:50 pm

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!
Kind regards,

Andreas Rozek

rozek
Posts: 151
Joined: Mon Jun 19, 2006 2:29 pm
Contact:

Post by rozek » Wed Jun 18, 2008 2:44 pm

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)?
Kind regards,

Andreas Rozek

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Contact:

Post by trevordevore » Wed Jun 18, 2008 2:58 pm

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
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

Post Reply