Just a curiosity question related to the "Function" command...

May 24, 2010
855
0
Northlake, Il
As I indicated in the subject line, I've found a work-around that is not particularly inconvenient. However, I do wonder why the situation is happening in the first place. So I will illustrate with an annotated "dump" of a complete TCC session:

Code:
   Fri  Dec 2, 2011  12:59:12p

ISO8601 plugin v1.1.1 loaded.
SafeChars plugin v1.5.7 loaded.

TCC  12.11.76   Windows 7 [Version 6.1.7601]
Copyright 2011  Rex Conn & JP Software Inc.  All Rights Reserved
Registered to Daniel Mathews

[Z:\]Function ReplaceChar=`%@Left[%@Dec[%2],%@UnQuote[%1]]%@UnQuote[%3]%@Right[-%2,
%@UnQuote[%1]]`
This is a much cut-down version of the "real" function which has some error handling and "special case" functionality which I've "stripped out" for demonstration purposes. As can be seen I am defining a rather simple function "ReplaceChar" that replaces the character in the string supplied as parameter one to the function by the string supplied as parameter three to the function at the position specified by parameter two to the function. (I will note here that the "position" is 1-based as opposed to 0-based as is true of many of Rex's built-in functions.)

The first example, which is a little bit "odd" but not at all a problem because it can easily be "worked around" (by putting the third parameter in double quotes):

Code:
[Z:\]Echo %@ReplaceChar[ABCDEF,4,This Will Replace the Fourth Character]
ABCThisEF
The second example showing that the previously-mentioned "work around" does, in fact work:

Code:
[Z:\]Echo %@ReplaceChar[ABCDEF,4,"This Will Replace the Fourth Character"]
ABCThis Will Replace the Fourth CharacterEF
The third example, where the previously-mentioned "work around" absolutely did not work (at all):

Code:
[Z:\]Echo %@ReplaceChar[ABCDEF,4,"        "]
ABCEF
As you can easily see in the above, the "D" was eliminated but absolutely no spaces were inserted in the character string at its former position.

And finally, the one that "worked" (by surrounding parameter three with back-quotes):

Code:
[Z:\]Echo %@ReplaceChar[ABCDEF,4,`        `]
ABC        EF
The question is "Why is that needed?" or maybe "Why dosn't the third example work?".
 

samintz

Scott Mintz
May 20, 2008
1,503
16
Solon, OH, USA
Dan,

The issues you are seeing relate to parameter parsing. Spaces, tabs,
commas, and forward slashes delimit parameters to a function.

So if I define a function like this:
function foo=`"%1" : "%2" : "%3$"`

And I invoke that function with comma delimited parameters vs space
delimited, I get the same results:
[R:\] echo %@foo[1,2,3]
"1" : "2" : "3"

[R:\] echo %@foo[1 2 3]
"1" : "2" : "3"

However, you'll notice that I used %3$ as my third argument in the
function definition. That means 3rd parameter plus all the rest.
So if I invoke that same function like this:

[R:\] echo %@foo[1 2 3 4 5 6 7 8 9]
"1" : "2" : "3 4 5 6 7 8 9"

Now, your second question about the spaces is caused for the same reason.
The parser removes trailing spaces.

Using your function as an example:
[R:\] echo %@ReplaceChar[test 3 " "]
tet

[R:\] echo %@ReplaceChar[test 3 " 123 "]
te 123t

Backquotes prevent the parser from removing the trailing spaces.

Look at the Parameter Quoting section of the help for a very detailed
description.

-Scott




As I indicated in the subject line, I've found a work-around that is not
particularly inconvenient. However, I do wonder why the situation is
happening in the first place. So I will illustrate with an annotated
"dump" of a complete TCC session:

Code:
Fri Dec 2, 2011 12:59:12p

ISO8601 plugin v1.1.1 loaded.
SafeChars plugin v1.5.7 loaded.

TCC 12.11.76 Windows 7 [Version 6.1.7601]
Copyright 2011 Rex Conn & JP Software Inc. All Rights Reserved
Registered to Daniel Mathews

[Z:\]Function
ReplaceChar=`%@Left[%@Dec[%2],%@UnQuote[%1]]%@UnQuote[%3]%@Right[-%2,
%@UnQuote[%1]]`
This is a much cut-down version of the "real" function which has some
error handling and "special case" functionality which I've "stripped out"
for demonstration purposes. As can be seen I am defining a rather simple
function "ReplaceChar" that replaces the character in the string supplied
as parameter one to the function by the string supplied as parameter three
to the function at the position specified by parameter two to the
function. (I will note here that the "position" is 1-based as opposed to
0-based as is true of many of Rex's built-in functions.)

The first example, which is a little bit "odd" but not at all a problem
because it can easily be "worked around" (by putting the third parameter
in double quotes):

Code:
[Z:\]Echo %@ReplaceChar[ABCDEF,4,This Will Replace the Fourth Character]
ABCThisEF
The second example showing that the previously-mentioned "work around"
does, in fact work:

Code:
[Z:\]Echo %@ReplaceChar[ABCDEF,4,"This Will Replace the Fourth Character"]
ABCThis Will Replace the Fourth CharacterEF
The third example, where the previously-mentioned "work around" absolutely
did not work (at all):

Code:
[Z:\]Echo %@ReplaceChar[ABCDEF,4," "]
ABCEF
As you can easily see in the above, the "D" was eliminated but absolutely
no spaces were inserted in the character string at its former position.

And finally, the one that "worked" (by surrounding parameter three with
back-quotes):

Code:
[Z:\]Echo %@ReplaceChar[ABCDEF,4,` `]
ABC EF
The question is "Why is that needed?" or maybe "Why dosn't the third
example work?".
 
May 24, 2010
855
0
Northlake, Il
Dan,

The issues you are seeing relate to parameter parsing. Spaces, tabs,
commas, and forward slashes delimit parameters to a function.

...

-Scott
Scott, of course you are correct. The major problem I have is that from my 30+ years of writing programs in about a dozen other programming languages I deeply expect (without really thinking about it) that double quotes will have more "meaning" than they often do in TCC, although, I am also aware, when I think about/remember it, that that is often not the case in TCC (something "inherited" from cmd.exe??? I haven't used that command processor on a "consistent" basis since about Windows 3.1, so I really wouldn't know.). A basic issue related to deeply-ingrained programming "habits"...
 
Similar threads
Thread starter Title Forum Replies Date
M A just out of curiosity question re @DiskTotal Support 4
M A just-out-of-curiosity question because it's ... Support 6
M Just an out-of-curiosity question re. "Do ... /P ..." Support 31
M Just an out-of-curiosity question the List command vs. the @Lines function... Support 13
M A question about file compression that is just out of curiosity... Support 2
M A purely out-of-curiosity question about @DriveType and @DriveTypeEx Support 2
M Question about the "Start" command that is admittedly not too important and just out of curiosity... Support 1
M Kind of a out-of-curiosity question re. the "Function" command... Support 5
R Curiosity - can TCC be a VBA Add-In? Support 2
vefatica A help curiosity Support 1
J assoc curiosity Support 1
C forum Posting Question... Support 3
C question re: Move Support 3
S How to? Upgrade Question: What supporting documentation is required at time of purchase? Support 2
Peter Murschall Documentation Question to %@PSHELL Support 4
Fross Tab Question Support 6
Fross Quick Function Question Support 17
Dick Johnson Question about the Touch command Support 0
rps Documentation "Copy+Paste+run" question Support 2
vefatica SFTP question Support 17
vefatica Question about IPWorks Support 0
C How to? SHORTCUT question..... Support 6
vefatica TPIPE, crash and question Support 1
C Question / Suggestion Support 1
MickeyF Another TPIPE question Support 6
vefatica Another @EVERYTHING question Support 4
mikea How to? %@everything[] question Support 10
A License Question for Single User Support 5
Jay Sage Help Correction (and Related Question) Support 0
S Elapsed time in TCC prompt question Support 0
H command line parsing question Support 5
C Forum question Support 2
B Newbie here, I have a question. Support 4
J Dumb Question ... Support 4
Timothy Byrd Updating from TC 9 to TC 17 - TCTOOLBAR question Support 2
M A possibly stupid question about the "del" command... Support 7
Fross Status Bar Time Question Support 1
A Stupid, obvious question - launch a BAT with TC Support 10
C How to? GLOBAL question Support 5
vefatica OT: VisualStudio question Support 1
vefatica Wildcard question Support 4
Dan Glynhampton v15 installer question Support 2
C Move question with Encrypted file... Support 6
M Maybe Maybe I'm being stupid (again), but a simple question... Support 4
C Message Board question Support 1
James Miller How to? filename completion question Support 1
C coding question Support 3
Frank question to FOLDERMONITOR Support 14
C Dumb titleprompt question Support 4
M Upgrade Question Support 2

Similar threads