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

May 24, 2010
855
0
Northlake, Il
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
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,461
88
Albuquerque, NM
prospero.unm.edu
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.)
 
May 20, 2008
3,515
4
Elkridge, MD, USA
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.
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,461
88
Albuquerque, NM
prospero.unm.edu
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.
 
May 24, 2010
855
0
Northlake, Il
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
 
May 24, 2010
855
0
Northlake, Il
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
Thread starter Title Forum Replies Date
vefatica Documentation Function parameters Support 0
Jay Sage Documentation Function @DRIVE Missing in Help Lists Support 0
Joe Caverly How to? Get name of current LIBRARY function? Support 4
x13 WeekNum function? Support 3
Joe Caverly No blank line in TEXT...ENDTEXT in a LIBRARY function Support 8
cgunhouse Ctrl-C from within a library function Support 6
C email[string] function Support 10
R FUNCTION to count NUMBER of files matching a pattern and specific length/not length Support 4
Dick Johnson Using the @instr function Support 5
vefatica '}' in a library function? Support 3
D Can the argument to RETURN be a function expansion? Support 3
J Trouble with @full[] function. Support 3
Fross Quick Function Question Support 17
vefatica Quoting @Function parameters? Support 0
Joe Caverly Debugging a LIBRARY function Support 10
Steve Pitts Documentation @CAPI function summary Support 0
x13 %x$ in function Support 3
thorntonpg @eval function, interesting results any explanation? Support 3
x13 Function to get interface user-defined name? Support 10
vefatica Fixed Help @FUNCTION, missing paragraph Support 8
Mordachai Variable followed by function constantly failing... Support 5
nickles Call a complex function Support 13
M Fixed Cannot use the "Browse..." function in "Find files/text" dialogbox Support 2
R Function #IDOW returns only 2 characters Support 10
T scrput `` in alias or function Support 2
S International characters - TYPE vs %@LINE function Support 5
JohnQSmith WAD @REPEAT function Support 15
vefatica WAD Nested variable expansion in PDIR's @function[*]? Support 13
R In addition to ALIAS /Z and FUNCTION /Z, HISTORY /Z? Support 1
A pdir function usage Support 3
nikbackm FUNCTION with variable number of arguments Support 17
S %@REGQUERY function Support 1
Dan Glynhampton Documentation v15 help: Error on @TRIMALL function page Support 0
M Why doesn't this @Field function work? Support 0
fpefpe How to? user function Support 1
M Why is this function getting these results in these circumstances? Support 6
A WAD function return values with parentheses prevent evaluation of additional functions Support 31
M Just an out-of-curiosity question the List command vs. the @Lines function... Support 13
M How to? Just verification for what I think I know regarding "Function"... Support 4
Frank How to? need help with "function" Support 10
Joe Caverly How to? Function Key With INPUT Support 5
M Kind of a out-of-curiosity question re. the "Function" command... Support 5
M Simple question about "Function" command... Support 4
M Just a curiosity question related to the "Function" command... Support 2
C not sure of windows registry function use Support 2
Phileosophos The @urlencode function doesn't seem to URL encode Support 16
J issue to have "if" inside function Support 3
Frank is the search-function in VIEW working? Support 2
M Weird @Index function result... Support 7
M . and .. directories counted by @files function Support 5

Similar threads