Accessing a local macro value in an outside macro

classic Classic list List threaded Threaded
25 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: Accessing a local macro value in an outside macro

David Marso
Administrator
Interesting observation and nice workaround Kirill!
Ah, the %^&%ing joys of Macro ;-)

"!MAIN.
*But with the help of !EVAL and being put in apostrophes, it works allright."

Kirill Orlov wrote
Andy,
Try this, to force macro at the right hand side of !LET.

define !F(!pos= !enclose('(',')'))
!conc('Andy ',!1)
!enddefine.

!F(Weeler).
*The F macro works.


define !MAIN()
!let !output= !F(Weeler)
echo !quote(!output).
!enddefine.

!MAIN.
*The F macro doesn't work correctly inside another macro when put in
right hand side of macro expression.


define !MAIN()
!let !output= !eval('!F(Weeler)')
echo !quote(!output).
!enddefine.

!MAIN.
*But with the help of !EVAL and being put in apostrophes, it works allright.

17.01.2014 0:44, Andy W ?????:
> This is starting to remind me of dialogue in Alice in Wonderland. While I can
> accept the fate that what I want to do is impossible, I'm having a hard time
> understanding why the compiler is fine and dandy to accept
>
> !F(!Input)
>
> But
>
> !LET !Output = !F(!Input)
>
> is impossible. Where does !LET go if not "code space" - outer space? Rural
> Idaho? The moon? I type !LET in the syntax window the same as I do any other
> command.
>
>
>
>
>
>
>
> -----
> Andy W
> [hidden email]
> http://andrewpwheeler.wordpress.com/
> --
> View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Accessing-a-local-macro-value-in-an-outside-macro-tp5723922p5723952.html
> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD
>
>
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Accessing a local macro value in an outside macro

Richard Ristow
In reply to this post by Kirill Orlov
At 01:48 AM 1/17/2014, Kirill Orlov wrote:

>Richard Ristow wrote:
>>When you write
>>!LET !Output = !F(!Input)
>>!F's output has gone to the SPSS code space, or code stream, and
>>the calling macro has no access to it.
>
>+1, Richard.
>But it is possible to make the text !F() produces a value accessible
>to the parent macro via !EVAL + quoting, - as I've shown above.
>!LET !Output = !EVAL('!F(!Input)')

Bravo, and thank you, Kirill!

Now that you've shown this, I realize that my own !MacEcho (appended)
uses the output of an !EVAL as within-macro text, and manipulates it.
I do not claim prior discovery; I've used !MacEcho for years, but
never recognized the importance of what I had.

========================================================
APPENDIX: !MacEcho - display a macro call and its result
========================================================
*  Macro /* !MacEcho */ is used to display the macro expansion   .
DEFINE !MacEcho(!POS !NOEXPAND !CMDEND)
    ECHO  !QUOTE(!CONCAT('     Call  : ',!1)).
    ECHO  !QUOTE(!CONCAT('     Result: ',!EVAL(!1))).
!ENDDEFINE.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Accessing a local macro value in an outside macro

David Marso
Administrator
In my mind the beauty of this is resolving return values from parameterized macros ;-)
I just thought of another way to do it using what I will refer to as a "callback".
Closely related to the Recursion ideas we were exploring recently.
One could of course set up the parameterization in !Main so it could have several new objects returned from !SUBMacro.  Of course with modification, !SubMacro could also "Chain" to other submacros etc...
One could also set up the parameterization of !SUBMacro so it would behave differently dependent upon values of !2.
I think this is actually much cleaner and more general than what Kirill built out.

DEFINE !SUBMacro ( !POS !CHAREND("/") / !POS !TOKENS(1) ).
!IF (!2 !EQ 1) !THEN
!Main !1 /Return /0 .
!IFEND
!ENDDEFINE .

DEFINE !Main ( !POS !CHAREND("/")
            / !POS !CHAREND("/") !DEFAULT ("")
            / !POS !CMDEND       !DEFAULT (1)   ).
!IF (!3 !EQ 1) !THEN
!SUBMacro !1  / 1 .
!IFEND
/* Process return value(s) from call to !SUBMacro */.
!IF (!3 !EQ 0) !THEN
ECHO !QUOTE(!CONCAT(!1," ",!2)).
!IFEND
!ENDDEFINE.

!Main GoingIn .

Result:
GoingIn Return

Richard Ristow wrote
At 01:48 AM 1/17/2014, Kirill Orlov wrote:
>Richard Ristow wrote:
>>When you write
>>!LET !Output = !F(!Input)
>>!F's output has gone to the SPSS code space, or code stream, and
>>the calling macro has no access to it.
>
>+1, Richard.
>But it is possible to make the text !F() produces a value accessible
>to the parent macro via !EVAL + quoting, - as I've shown above.
>!LET !Output = !EVAL('!F(!Input)')

Bravo, and thank you, Kirill!

Now that you've shown this, I realize that my own !MacEcho (appended)
uses the output of an !EVAL as within-macro text, and manipulates it.
I do not claim prior discovery; I've used !MacEcho for years, but
never recognized the importance of what I had.

========================================================
APPENDIX: !MacEcho - display a macro call and its result
========================================================
*  Macro /* !MacEcho */ is used to display the macro expansion   .
DEFINE !MacEcho(!POS !NOEXPAND !CMDEND)
    ECHO  !QUOTE(!CONCAT('     Call  : ',!1)).
    ECHO  !QUOTE(!CONCAT('     Result: ',!EVAL(!1))).
!ENDDEFINE.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Accessing a local macro value in an outside macro

David Marso
Administrator
A modification which passes the callback macro into !SubMacro ;-)
DEFINE !SUBMacro ( !POS !CHAREND("/") / !POS !CHAREND("/") / !POS !TOKENS(1)).
!IF (!3 !EQ 1) !THEN
!UNQUOTE(!2) !1 /Return /0 .
!IFEND
!ENDDEFINE .

DEFINE !Main ( !POS !CHAREND("/")
             / !POS !CHAREND("/") !DEFAULT ("")
             / !POS !CMDEND       !DEFAULT (1)   ).
!IF (!3 !EQ 1) !THEN
!SUBMacro !1  / !2 / 1 .
!IFEND
!IF (!3 !EQ 0) !THEN
ECHO !QUOTE(!CONCAT(!1," ",!2)).
!IFEND
!ENDDEFINE.
!Main GoingIn / '!Main' / .


On Sun, Jan 19, 2014 at 6:19 PM, David Marso [via SPSSX Discussion] <[hidden email]> wrote:
In my mind the beauty of this is resolving return values from parameterized macros ;-)
I just thought of another way to do it using what I will refer to as a "callback".
Closely related to the Recursion ideas we were exploring recently.
One could of course set up the parameterization in !Main so it could have several new objects returned from !SUBMacro.  Of course with modification, !SubMacro could also "Chain" to other submacros etc...
One could also set up the parameterization of !SUBMacro so it would behave differently dependent upon values of !2.
I think this is actually much cleaner and more general than what Kirill built out.

DEFINE !SUBMacro ( !POS !CHAREND("/") / !POS !TOKENS(1) ).
!IF (!2 !EQ 1) !THEN
!Main !1 /Return /0 .
!IFEND
!ENDDEFINE .

DEFINE !Main ( !POS !CHAREND("/")
            / !POS !CHAREND("/") !DEFAULT ("")
            / !POS !CMDEND       !DEFAULT (1)   ).
!IF (!3 !EQ 1) !THEN
!SUBMacro !1  / 1 .
!IFEND
/* Process return value(s) from call to !SUBMacro */.
!IF (!3 !EQ 0) !THEN
ECHO !QUOTE(!CONCAT(!1," ",!2)).
!IFEND
!ENDDEFINE.

!Main GoingIn .

Result:
GoingIn Return

Richard Ristow wrote
At 01:48 AM 1/17/2014, Kirill Orlov wrote:

>Richard Ristow wrote:
>>When you write
>>!LET !Output = !F(!Input)
>>!F's output has gone to the SPSS code space, or code stream, and
>>the calling macro has no access to it.
>
>+1, Richard.
>But it is possible to make the text !F() produces a value accessible
>to the parent macro via !EVAL + quoting, - as I've shown above.
>!LET !Output = !EVAL('!F(!Input)')

Bravo, and thank you, Kirill!

Now that you've shown this, I realize that my own !MacEcho (appended)
uses the output of an !EVAL as within-macro text, and manipulates it.
I do not claim prior discovery; I've used !MacEcho for years, but
never recognized the importance of what I had.

========================================================
APPENDIX: !MacEcho - display a macro call and its result
========================================================
*  Macro /* !MacEcho */ is used to display the macro expansion   .
DEFINE !MacEcho(!POS !NOEXPAND !CMDEND)
    ECHO  !QUOTE(!CONCAT('     Call  : ',!1)).
    ECHO  !QUOTE(!CONCAT('     Result: ',!EVAL(!1))).
!ENDDEFINE.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"



If you reply to this email, your message will be added to the discussion below:
http://spssx-discussion.1045642.n5.nabble.com/Accessing-a-local-macro-value-in-an-outside-macro-tp5723922p5724006.html
To unsubscribe from Accessing a local macro value in an outside macro, click here.
NAML

Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Accessing a local macro value in an outside macro

David Marso
Administrator
An example of "chaining" macros and parsing a stack of return values.
--
DEFINE !S2( !POS !CHAREND("/") / !POS !CHAREND("/") ).
!LET !MSG="S2"
!UNQUOTE(!HEAD(!2)) !CONCAT (!1," ",!QUOTE(!MSG))/!TAIL(!2) .
!ENDDEFINE .

DEFINE !SUBMacro ( !POS !CHAREND("/") / !POS !CHAREND("/") ).
!LET !MSG="SubMacro"
!UNQUOTE(!HEAD(!2)) !CONCAT(!1," ",!QUOTE(!MSG))/!TAIL(!2) .
!ENDDEFINE .

/* Call stack / Macro Chain / State */.
DEFINE !Main ( !POS !CHAREND("/")
             / !POS !CHAREND("/") !DEFAULT ("")
             / !POS !CMDEND       !DEFAULT (1)   ).
!LET !MSG="Main"
!IF (!2 !NE !NULL) !THEN
!UNQUOTE(!HEAD (!2)) !CONCAT(!1," ",!QUOTE(!MSG)) / !TAIL(!2) .
!IFEND
!IF (!2 !EQ !NULL) !THEN
!LET !Return=!CONCAT(!1," ",!2," ",!QUOTE(!MSG))
ECHO !QUOTE(!CONCAT('Return=',!Return)).
!DO !Arg !IN (!Return)
ECHO !QUOTE(!arg).
!DOEND
!IFEND
!ENDDEFINE.
SET PRINTBACK Off MPRINT Off.
!Main 'GoingIn' /'!SubMacro' '!S2' '!Main'  .

--- OUTPUT---
Return='GoingIn' 'Main' 'SubMacro' 'S2'  'Main'
'GoingIn'
'Main'
'SubMacro'
'S2'
'Main'
===
David Marso wrote
A modification which passes the callback macro into !SubMacro ;-)
DEFINE !SUBMacro ( !POS !CHAREND("/") / !POS !CHAREND("/") / !POS
!TOKENS(1)).
!IF (!3 !EQ 1) !THEN
!UNQUOTE(!2) !1 /Return /0 .
!IFEND
!ENDDEFINE .

DEFINE !Main ( !POS !CHAREND("/")
             / !POS !CHAREND("/") !DEFAULT ("")
             / !POS !CMDEND       !DEFAULT (1)   ).
!IF (!3 !EQ 1) !THEN
!SUBMacro !1  / !2 / 1 .
!IFEND
!IF (!3 !EQ 0) !THEN
ECHO !QUOTE(!CONCAT(!1," ",!2)).
!IFEND
!ENDDEFINE.
!Main GoingIn / '!Main' / .


On Sun, Jan 19, 2014 at 6:19 PM, David Marso [via SPSSX Discussion] <
[hidden email]> wrote:

> In my mind the beauty of this is resolving return values from
> parameterized macros ;-)
> I just thought of another way to do it using what I will refer to as a
> "callback".
> Closely related to the Recursion ideas we were exploring recently.
> One could of course set up the parameterization in !Main so it could have
> several new objects returned from !SUBMacro.  Of course with modification,
> !SubMacro could also "Chain" to other submacros etc...
> One could also set up the parameterization of !SUBMacro so it would behave
> differently dependent upon values of !2.
> I think this is actually much cleaner and more general than what Kirill
> built out.
>
> DEFINE !SUBMacro ( !POS !CHAREND("/") / !POS !TOKENS(1) ).
> !IF (!2 !EQ 1) !THEN
> !Main !1 /Return /0 .
> !IFEND
> !ENDDEFINE .
>
> DEFINE !Main ( !POS !CHAREND("/")
>             / !POS !CHAREND("/") !DEFAULT ("")
>             / !POS !CMDEND       !DEFAULT (1)   ).
> !IF (!3 !EQ 1) !THEN
> !SUBMacro !1  / 1 .
> !IFEND
> /* Process return value(s) from call to !SUBMacro */.
> !IF (!3 !EQ 0) !THEN
> ECHO !QUOTE(!CONCAT(!1," ",!2)).
> !IFEND
> !ENDDEFINE.
>
> !Main GoingIn .
>
> Result:
> GoingIn Return
>
> Richard Ristow wrote
> At 01:48 AM 1/17/2014, Kirill Orlov wrote:
> >Richard Ristow wrote:
> >>When you write
> >>!LET !Output = !F(!Input)
> >>!F's output has gone to the SPSS code space, or code stream, and
> >>the calling macro has no access to it.
> >
> >+1, Richard.
> >But it is possible to make the text !F() produces a value accessible
> >to the parent macro via !EVAL + quoting, - as I've shown above.
> >!LET !Output = !EVAL('!F(!Input)')
>
> Bravo, and thank you, Kirill!
>
> Now that you've shown this, I realize that my own !MacEcho (appended)
> uses the output of an !EVAL as within-macro text, and manipulates it.
> I do not claim prior discovery; I've used !MacEcho for years, but
> never recognized the importance of what I had.
>
> ========================================================
> APPENDIX: !MacEcho - display a macro call and its result
> ========================================================
> *  Macro /* !MacEcho */ is used to display the macro expansion   .
> DEFINE !MacEcho(!POS !NOEXPAND !CMDEND)
>     ECHO  !QUOTE(!CONCAT('     Call  : ',!1)).
>     ECHO  !QUOTE(!CONCAT('     Result: ',!EVAL(!1))).
> !ENDDEFINE.
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] <http://user/SendEmail.jtp?type=node&node=5724006&i=0> (not
> to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD
>
>  Please reply to the list and not to my personal email.
> Those desiring my consulting or training services please feel free to
> email me.
> ---
> "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos
> ne forte conculcent eas pedibus suis."
> Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in
> abyssum?"
>
>
> ------------------------------
>  If you reply to this email, your message will be added to the discussion
> below:
>
> http://spssx-discussion.1045642.n5.nabble.com/Accessing-a-local-macro-value-in-an-outside-macro-tp5723922p5724006.html
>  To unsubscribe from Accessing a local macro value in an outside macro, click
> here<http://spssx-discussion.1045642.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5723922&code=ZGF2aWQubWFyc29AZ21haWwuY29tfDU3MjM5MjJ8LTkzOTgzMDYxMw==>
> .
> NAML<http://spssx-discussion.1045642.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
12