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

Re: Understanding Writing Subroutines

Expand Messages
  • rpdooling
    Bob, ... I have never seen that. That IS a nifty trick. Rick H=first letter ^!SetArray %items%=red;orange;yellow;green;blue;indigo;violet
    Message 1 of 11 , Apr 17, 2006
    • 0 Attachment
      Bob,

      >> ^$grabfirstletter(^%items%)$ = ^%items%^%NL%

      I have never seen that. That IS a nifty trick.

      Rick


      H=first letter
      ^!SetArray %items%=red;orange;yellow;green;blue;indigo;violet
      ^$grabfirstletter(^%items%)$ = ^%items%^%NL%
      ^!SetArray %items%=Mediocrity;Ego;Limits;Vanity;Incompetence;Name-calling
      ^$grabfirstletter(^%items%)$ = ^%items%^%NL%

      H=_grabfirstletter
      ^!SetArray %wordlist%=^&
      ^!Set %count%=1;%Result%=""
      :Loop
      ^!Set %Result%=^%Result%^$StrCopyLeft("^%wordlist^%count%%";1)$
      ^!Inc %count%
      ^!If ^%count% <= ^%items0% Loop

      --- In ntb-clips@yahoogroups.com, "Bob McAllister" <fortiter@...> wrote:
      >
      > On 4/16/06, Don - htmlfixit.com <don@...> wrote:
      > > Here is how I have taken to using "subroutines" and
      > > as you see I am using an array to pass the parameters.
      > >
      > > H="first letter"
      > > ;set variables to be passed as parameters
      > > ^!SetArray %items%=red;orange;yellow;green;blue;indigo;violet
      > > ;call "subroutine"
      > > ^!Clip "grabfirstletters"
      > > ^!SetArray
      %items%=Mediocrity;Ego;Limits;Vanity;Incompetence;Name-calling
      > > ;call "subroutine"
      > > ^!Clip "grabfirstletters"
      >
      > Don
      >
      > I have modified your two clips to use custom functions as below.
      >
      > H=first letter
      > ^!SetArray %items%=red;orange;yellow;green;blue;indigo;violet
      > ^$grabfirstletter(^%items%)$ = ^%items%^%NL%
      > ^!SetArray
      %items%=Mediocrity;Ego;Limits;Vanity;Incompetence;Name-calling
      > ^$grabfirstletter(^%items%)$ = ^%items%^%NL%
      >
      > H=_grabfirstletter
      > ^!SetArray %wordlist%=^&
      > ^!Set %count%=1;%Result%=""
      > :Loop
      > ^!Set %Result%=^%Result%^$StrCopyLeft("^%wordlist^%count%%";1)$
      > ^!Inc %count%
      > ^!If ^%count% <= ^%items0% Loop
      >
      > The key is the use of the "special variable" %Result% in the
      > definition of the custom function. That will be what is returned at
      > the end of the sub-routine and its value printed by the top-level clip
      > at the point where the custom function was called.
      >
      > This is a general case where the "content" of your array variable
      > %items% is passed to the custom function (as ^&) and placed into a new
      > variable. It works regardless of the source of the passed parameter.
      >
      > In your situation where tou already know the name of the variable
      > holding the data on which the function is to operate, I can
      > short-circuit by using that same variable in the custom function (and
      > so effectively pass an empty parameter).
      >
      > The following clips implement that special case relying on knowing the
      > variable name in calling routine.
      > H=flspecialcase
      > ^!SetArray %items%=red;orange;yellow;green;blue;indigo;violet
      > ^$grabfirstletterx()$ = ^%items%^%NL%
      > ^!SetArray
      %items%=Mediocrity;Ego;Limits;Vanity;Incompetence;Name-calling
      > ^$grabfirstletterx()$ = ^%items%^%NL%
      >
      > H=_grabfirstletterx
      > ^!Set %count%=1;%Result%=""
      > :Loop
      > ^!Set %Result%=^%Result%^$StrCopyLeft("^%items^%count%%";1)$
      > ^!Inc %count%
      > ^!If ^%count% <= ^%items0% Loop
      >
      > There may be circumstances where you prefer this method.
      >
      > Bob
      >
    Your message has been successfully submitted and would be delivered to recipients shortly.