@Words Incorrect?

Hello everyone,

I seem to be having issues with @Words not returning correctly if the item is quoted. Am I using @Words incorrectly or is there an issue with it? This is obvious enough that I would have assume someone else would have come across it. I did find a post from last year where Vince had some concerns with @Words working correctly.

Code:
TCC  22.00.39 x64   Windows 10 [Version 10.0.16299]
TCC Build 39   Windows 10 Build 16299

D:\Users\Michael>echo %@Words[abc]
1

D:\Users\Michael>echo %@Words[abc xyz]
2

D:\Users\Michael>echo %@Words["abc"]
0

D:\Users\Michael>echo %@Words["abc xyz"]
1
The same seems to occur with @Fields. It seems as if the first abc is getting dropped if there are quotes.

Thanks for your help.

Michael
 
May 20, 2008
9,274
62
Syracuse, NY, USA
I see something slightly different with the 32-bit version. It still doesn't look quite right.
Code:
v:\> ver

TCC  22.00.39   Windows 7 [Version 6.1.7601]

v:\> echo %@words[abc]
1

v:\> echo %@words[abc xyz]
2

v:\> echo %@words["abc"]
1

v:\> echo %@words["abc xyz"]
0
And I see oddities like this also.
Code:
v:\> echo %@words["abc xyz"]
0

v:\> echo %@words["ab xyz"]
1
 
Last edited:
May 20, 2008
9,274
62
Syracuse, NY, USA
It seems rather random.
Code:
v:\> unset test & do char in /c abcdefghijklmnopqrstuvwxyz (set test=%[test]%char & echo  %test^t%@words["%test"] )
 a      1
 ab     1
 abc    0
 abcd   0
 abcde  1
 abcdef 0
 abcdefg        0
 abcdefgh       0
 abcdefghi      0
 abcdefghij     0
 abcdefghijk    0
 abcdefghijkl   0
 abcdefghijklm  1
 abcdefghijklmn 0
 abcdefghijklmno        0
 abcdefghijklmnop       0
 abcdefghijklmnopq      0
 abcdefghijklmnopqr     0
 abcdefghijklmnopqrs    0
 abcdefghijklmnopqrst   0
 abcdefghijklmnopqrstu  1
 abcdefghijklmnopqrstuv 1
 abcdefghijklmnopqrstuvw        0
 abcdefghijklmnopqrstuvwx       1
 abcdefghijklmnopqrstuvwxy      0
 abcdefghijklmnopqrstuvwxyz     0
 
May 20, 2008
9,274
62
Syracuse, NY, USA
@FIELDS is equally interesting.
Code:
v:\> unset test & do char in /c abcdefghijklmnopqrstuvwxyz (set test=%[test]%char & echo  %test^t%@field
s["%test"] )
 a      1
 ab     1
 abc    0
 abcd   0
 abcde  1
 abcdef 3
 abcdefg        3
 abcdefgh       3
 abcdefghi      3
 abcdefghij     0
 abcdefghijk    0
 abcdefghijkl   0
 abcdefghijklm  1
 abcdefghijklmn 3
 abcdefghijklmno        0
 abcdefghijklmnop       0
 abcdefghijklmnopq      2
 abcdefghijklmnopqr     2
 abcdefghijklmnopqrs    0
 abcdefghijklmnopqrst   0
 abcdefghijklmnopqrstu  1
 abcdefghijklmnopqrstuv 4
 abcdefghijklmnopqrstuvw        6
 abcdefghijklmnopqrstuvwx       0
 abcdefghijklmnopqrstuvwxy      3
 abcdefghijklmnopqrstuvwxyz     0
 
May 20, 2008
9,274
62
Syracuse, NY, USA
I suppose $WORDS and @FIELDS are interpreting their first parameter as a list of separators (because it's quoted, as documented). In that case, I don't know where it's getting a second paramater. Perhaps an error message is in order when the first parameter is interpreted an a separator list and the comma after it is missing. Fix it all by specifying a separator lists.
Code:
v:\> unset test & do char in /c abcdefghijklmnopqrstuvwxyz (set test=%[test]%char & echo  %test^t%@words[" ","%test"] )
a      1
ab     1
abc    1
abcd   1
abcde  1
abcdef 1
abcdefg        1
abcdefgh       1
abcdefghi      1
abcdefghij     1
abcdefghijk    1
abcdefghijkl   1
abcdefghijklm  1
abcdefghijklmn 1
abcdefghijklmno        1
abcdefghijklmnop       1
abcdefghijklmnopq      1
abcdefghijklmnopqr     1
abcdefghijklmnopqrs    1
abcdefghijklmnopqrst   1
abcdefghijklmnopqrstu  1
abcdefghijklmnopqrstuv 1
abcdefghijklmnopqrstuvw        1
abcdefghijklmnopqrstuvwx       1
abcdefghijklmnopqrstuvwxy      1
abcdefghijklmnopqrstuvwxyz     1
Or, as in the original example,
Code:
v:\> echo %@words[abc]
1

v:\> echo %@words[abc xyz]
2

v:\> echo %@words[" ","abc"]
1

v:\> echo %@words[" ","abc xyz"]
1
 

rconn

Administrator
Staff member
May 14, 2008
10,974
97
WAD. If the first argument to @WORDS begins with a double quote, it is assumed to be a separator list. If you want to pass a double quoted argument to @WORDS, you need to provide two arguments, one for the separator and one for the double quote.

But I can't imagine why you would want to pass a double quoted argument, since that would always return "1".
 
May 20, 2008
9,274
62
Syracuse, NY, USA
Thanks Vince. Specifying the separator did solve my problem. But it is odd since the separator is optional and quoted strings are not uncommon.

Michael
I agree, but as it is, apparently, if the first parameter is quoted, It's taken as a parameter list.
 
That smells of http://xyproblem.info/
You should fix the way you obtain names to not contain unnecessary characters, or use @UNQUOTES[]
I don't believe this is an xy problem and I believe it does not work intuititively, but I'm happy knowing how to solve it.

Not quoting them can be a challenge. I'm using this as part of my GetOpt script which often takes filenames and directory names as parameters. If you tab complete a directory or file, it's quoted by TCC if needed. Editing the command line is inconvenient and if I use Unquote[] I could get in trouble with directory names that have commas, square brackets, etc in them.

I think Vince had the best answer and that is to just force the optional first parameter.

Michael

PS: If you are curious, here is a link to my GetOpt.btm command line processor.
 
Hi Vince,

I use the following code to tell me how many command line options I have to loop through:

Code:
set getopt_TotalArgs=%@Words[" ",%*]
For example, if I run the btm with several Tab Completed files and directories:

Code:
C:\>getopt.btm Apps\ BOOTNXT "Documents and Settings"\ "Program Files"

--- GetOpt Debug Mode Activated ------------------------------------------------

Working with command line:
  Apps\ BOOTNXT "Documents and Settings"\ "Program Files"

GetOpt is processing 4 arguments:

<the rest of the output has been culled>
 
Aug 23, 2010
354
5
@UNQUOTES, not @UNQUOTE !
These are two different functions with greatly different results!

And avoid %* like plague. It is subject to DuplicateBugs=Yes. Use %$ instead.