Loading ...
Sorry, an error occurred while loading the content.

Re: [Clip] Re: Writing Subroutines in Notetab.

Expand Messages
  • Bob McAllister
    ... Bob What you are hoping to do is certainly possible using a custom function so long as you switch from Replace to StrReplace. Here are your clips reworked.
    Message 1 of 7 , Apr 12, 2006
    • 0 Attachment
      On 4/13/06, Bob Gorman <bgorman@...> wrote:
      >
      > I've also been experimenting with this, but I'm not quite successful as yet. I may be missing something very basic about Clips, and what they operate on.
      >
      > Originally, a long time ago, I wrote a clip which does exactly what I want
      > it to do, but it does it to text that is selected in a document. I now want
      > it to do it to the contents of a variable.
      >
      > Original Clip:
      > H="Me --> you"
      > ^!Replace "me" >> "you" HISA
      > ^!Replace "I was" >> "you were" HISA
      > ^!Replace "I'm" >> "you're" HISA
      > ^!Replace "I" >> "you" HISA
      > ^!Replace "myself" >> "your self" HISA
      > ^!Replace "my" >> "your" HISA
      > ^!Replace "mine" >> "yours" HISA
      > So if I select:
      > I love my life.
      > it changes it to:
      > you love your life.
      > Perfect!
      >
      > My attempt to use this clip on a variable instead of selected text:
      > H="Reverse Ans"
      > ^!Set %Ans%=^?[(T=W)Answer=]
      > ^!Set %rAns%=^!Clip "Me --> you" ^%Ans%
      > Original:
      > ^%Ans%
      > Reversed:
      > ^%rAns%
      >
      > The first command works fine and assigns the text to %Ans%.
      > The second line sets the variable %rAns% to the text of my clip :-(
      >

      Bob

      What you are hoping to do is certainly possible using a custom
      function so long as you switch from Replace to StrReplace.

      Here are your clips reworked.

      H="m2y"
      ^!Set %trans%=^$StrReplace("me";"you";"^&";0;1)$
      ^!Set %trans%=^$StrReplace("I was";"you were";"^%trans%";0;1)$
      ^!Set %trans%=^$StrReplace("I'm";"you're";"^%trans%";0;1)$
      ^!Set %trans%=^$StrReplace("I";"you";"^%trans%";0;1)$
      ^!Set %trans%=^$StrReplace("myself";"yourself";"^%trans%";0;1)$
      ^!Set %trans%=^$StrReplace("my";"your";"^%trans%";0;1)$
      ^!Set %Result%=^$StrReplace("mine";"yours";"^%trans%";0;1)$

      H="Call m2y"
      ^!Set %Ans%=^?{Text to be manipulated}
      Original:
      ^%Ans%
      Reversed:
      ^$m2y(^%Ans%)$

      Note that when a custom function (in this case m2y) is called by
      another clip, any parameters passed come in identified as ^&. So that
      is the string on which the first StrReplace acts. The variable %trans%
      is one i created to make the cascade through seven string replacements
      manageable. If you are better at counting brackets than I am, you
      could put them all in a single line nested seven deep (and avoid the
      use of %trans% entirely). The 0;1 parameters at the end of the line
      are to make the replacement case insensitive but whole words only.

      The key to using a custom function is to assign the output to the
      special variable %Result%.

      When calling the custom function (or sub-routine) you do not need to
      use the ^!Clip command (even though you are calling a clip). When
      NoteTab does not recognise the function name ^$m2y()$ it looks for a
      clip m2y in your current library and starts it.

      So the line ^$m2y(^%Ans%)$ is doing all the work. It takes the current
      value of %Ans% and passes it to the m2y clip (as ^&). Then when the
      subroutine ends, that line has the value held in %Result% which is
      printed into the current document.

      So you get
      Original:
      I was lost but now I'm found.
      Reversed:
      you were lost but now you're found.

      I hope this helps. Custom Functions really do give you enormous power
      to create sub-routine-like structures in clips. It is certainly worth
      persevering.

      Bob McAllister
    • Bob McAllister
      ... While continuing to tinker with my two clips, I thought it might be useful to show how they work as one and so demonstrate the advantages of converting a
      Message 2 of 7 , Apr 12, 2006
      • 0 Attachment
        On 4/13/06, Bob McAllister <fortiter@...> wrote:
        >
        > Note that when a custom function (in this case m2y) is called by
        > another clip, any parameters passed come in identified as ^&. So that
        > is the string on which the first StrReplace acts. The variable %trans%
        > is one i created to make the cascade through seven string replacements
        > manageable. If you are better at counting brackets than I am, you
        > could put them all in a single line nested seven deep (and avoid the
        > use of %trans% entirely). The 0;1 parameters at the end of the line
        > are to make the replacement case insensitive but whole words only.
        >
        > The key to using a custom function is to assign the output to the
        > special variable %Result%.
        >

        While continuing to tinker with my two clips, I thought it might be
        useful to show how they work as one and so demonstrate the advantages
        of converting a complex "function" that is likely to be re-used into a
        custom function that sits in another clip.

        H="Transform Me--->You"
        ^!Set %Ans%=^?{Text to be manipulated}
        Original:
        ^%Ans%
        Transformed:
        ;Everything after this is one LONG LINE
        ^$StrReplace("mine";"yours";"^$StrReplace("my";"your";"^$StrReplace("myself";"yourself";"^$StrReplace("I";"you";"^$StrReplace("I'm";"you're";"^$StrReplace("I
        was";"you were";"^$StrReplace("me";"you";"^%Ans%";0;1)$
        ";0;1)$";0;1)$";0;1)$";0;1)$ ";0;1)$ ";0;1)$

        Cheers

        Bob
      • abairheart
        ... work ... I m not sure what NoteTab versions y all are using, but Pro has a string repace function, which will search the string for a substring, and
        Message 3 of 7 , Apr 13, 2006
        • 0 Attachment
          --- In ntb-clips@yahoogroups.com, Bob Gorman <bgorman@...> wrote:
          >
          > Don & Alan,
          >
          > Thank you both.
          > I knew I could as you both suggest create a new document and do the
          work
          > there, but it just seemed like there might be a more elegant way.


          I'm not sure what NoteTab versions y'all are using, but Pro has a
          string repace function, which will search the string for a substring,
          and replace it. You CAN use a variable for each of these arguments:

          ^$StrReplace("long string";"short string";"very long string";0;0)$

          I.e.

          ^!Set %VLS%=Very long string
          ^!Set %ls%=long string; %ss%=short string
          ^!Set %VLS%=^$StrReplace("^%ls%";"^%ss%";"^%vls%";0;0)$
          ^!Info ^%vls%

          Abair
        Your message has been successfully submitted and would be delivered to recipients shortly.