1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to? "Function" and "%[xxx]"

Discussion in 'Support' started by mathewsdw, Dec 29, 2011.

  1. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    855
    Likes Received:
    0
    To start, I'll spare the reader from why I want to do this; and so there are hopefully absolutely no unanswered questions about what I am trying to do and the results I am getting, here is a complete transcript of a function definition and the results of invoking said function:

    Function Sample=`^n %%$: "%$"^n %%2: "%2"^n %%#: "%#"^n %%-3%: "%-3$"^n%%[1]: "%[1]"`

    [Z:\]Echo %@Sample[abc fghij nopqrs uvwxyz]

    %$: "abc fghij nopqrs uvwxyz"
    %2: "fghij"
    %#: "4"
    %-3: "fghij"
    %[1]: ""

    And to quickly summarize if it is not obvious, "%[1]" returns a null string.

    Since, as far as I know I really need this functionality and I no of no other (simple!) way to get it, am I doing something wrong or is there a simple way to fix it (i.e., %[1] returning the first argument to the function), or will I just plain have to "investigate" the far more complicated option(s)?

    - Dan
     
  2. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,280
    Likes Received:
    38
    Unless you first define an environment variable named 1, e.g. SET 1=FOOBAZ. As far as I know, that bracketed-name notation is intended only for environment variables, not for function arguments.

    (I presume that what you are ultimately trying to do is to iterate over arguments in order. No, I don't know how you would do that; but then I've never had a need to do that myself.)
     
  3. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,732
    Likes Received:
    81
    Charles is correct -- you cannot use the brackets with positional argument references (or indirectly reference a function position argument). I don't know why you would need to (it has never come up before).
     
  4. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    AFAIK the SHIFT command cannot be used in a function (it would affect the batch file itself where the function is used), it is very difficult to write a function with a variable number of arguments (optional arguments). The %[%n] style could assist. However, this may have nothing to do with the OP's goal.
     
  5. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,280
    Likes Received:
    38
    The user-defined function mechanism just does a string substitution. It's a nice, reasonably powerful string substitution facility, but it is not a programming language. If you really need to put code inside a user-defined function, I think the right way is to write it as a batch file/program/script/plugin and then, if needed, slurp its output with @EXECSTR.
     
  6. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    855
    Likes Received:
    0
    Thank you, guys, you answered the question, and @ExecStr was exactly the "alternative" solution I was trying to avoid for "performance" reasons more than anything else. And I won't to into great detail here (lest you think I'm even stranger than you already think I am :)), but what I was trying to do was change the behavior of an internal command such that it "defaulted" two other parameters (or, more specifically, "inverted" their meanings) if a third parameter was not specified, and left the parameters retain their original meaning if that third parameter was specified. This was because I found the display without those parameters to be very annoying and unhelpful (because of my vision issues and that it "wasted" screen space I absolutely did not want to waste) if the third parameter was not specified, whereas if the third parameter is specified the effect(s) of those other two parameters made the resulting display relatively useless, and I was hoping not to have to remember to supply them in the first case. (And Rex, this is in no sense a complaint or criticism. What I want to do is specifically related to my vision issues, and a "normal" person in "normal" circumstances would not have these issue(s).) But thank you all and on to the next try...

    - Dan
     
  7. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,178
    Likes Received:
    11
    Why can't you just use an alias?
     
  8. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    855
    Likes Received:
    0
    Scott, I've been otherwise completely occupied lately (and actually still am!), but the reason I didn't want to use an alias (at this point I have 54 aliases defined, I just looked) is because I didn't want to have to remember to use the alias in one case vs. the other (as I've said many times in the past, my memory ain't good - absolutely not a joke; and I am also very much a "creature of habit"). And the previous is actually completely besides the point. I wanted the command (or alias or whatever) to do one thing if a parameter was supplied and another thing if that parameter was not supplied (i.e., invoking a command with different parameters whose defaults depended on the "appearance" of yet another parameter(s)) - I wanted to be able to use one "command" (not to have to remember which one to use) that behaved in one way with respect to other parameters (particularly in that their "meanings" would effectively be "inverted") if another parameter was coded and another way (the other parameters retained their "original", "non-inverted" meanings) if that parameter was not coded.

    And, fortunately for me, "@ExecStr" got the job done and only slightly inconveniently.

    - Dan
     

Share This Page