Welcome!

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

SignUp Now!

@index not working when the haystack string contains a comma

Aug
47
2
Please look at the following statements.
C:\Program Files\JPSoft\TCMD31\ > set text=Heines, Jesse C:\Program Files\JPSoft\TCMD31\ > echo %@index[%text,J] 0 C:\Program Files\JPSoft\TCMD31\ > set text=Heines Jesse C:\Program Files\JPSoft\TCMD31\ > echo %@index[%text,J] 7

Clearly, when the haystack string (string1 in the documentation for @INDEX) contains a comma, @index fails. I have tried all the escaping the comma to no avail. To me, this is clearly a bug. Am I wrong? Am I misunderstanding something significant?

Thank you.
 
If you're trying to include the escape character in the environment variable you need to double it:
Code:
c:\>set text=Heines^^, Jesse

c:\>set text
Heines^, Jesse

c:\>echo %@index[%text,J]
8

It should also work if you quote the string, but that seems to return a value that is one higher than expected, which may be a bug:
Code:
c:\>echo %@index["Heines, Jesse",J]
9

c:\>set text="Heines, Jesse"

c:\>echo %@index[%text,J]
9

c:\>set text=Heines, Jesse

c:\>echo %@index["%text",J]
9
 
According to the help, @INDEX["%text",J] is the way to go. And, yes, the quotes remain and the index could be considered off by one. But that's to be expected. Though double quotes group words into single parameters, they're rarely, if ever, removed. As for why this gives 0 ...

Code:
v:\> set name=Heines, Jesse

v:\> echo %@index[%name,J]
0

... I can only speculate. After variable expansion, it looks like this.

Code:
echo %@index[Heines, Jesse,J]

@INDEX sees the optional third parameter, 'J' (called 'n' in the help), tries to turn it into an integer, and gets 0 (the default for many string_to_integer functions). So @INDEX tries to count the number of occurrences of " Jesse" in "Heines" and comes up with 0. It wouldn't help much if @INDEX tried to validate that third parameter. Even if it realized that 'J' couldn't be turned into an integer the best you could hope for is an error message.

I'm confident that there are arguments against @INDEX ignoring surrounding double-quotes.
 
Back
Top
[FOX] Ultimate Translator
Translate