Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

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

May
855
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
 
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.

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.)
 
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).
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.
 
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.

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.
 
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
 
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
 

Similar threads

Back
Top