I'm getting odd behaviors with comparisons like:
::msg ("2" < "35a");
::msg ("2" < "52=a");
::msg ("2d" < "52=a");
They all return true. There are also some others too.
Strings and Numbers Comparison Errors
Forum rules
You might want to read this if you're looking for priority service
Please Include The Following Information
1) Your XYplorer Version (e.g., v28.00.0801)
2) Your Windows Version (e.g., Win 11)
3) Your Screen Scaling Percentage (e.g., 125%).
Use Your Profile Or Signature
We strongly recommend adding your Windows Version and Screen Scaling Percentage to the Location field in your Profile or to your Signature. That way, you only have to type them once, and we won't have to search for that vital information.
How To Attach Images
When attaching an Image, please use the Attachment tab at the bottom of your post and click "Add files".
You might want to read this if you're looking for priority service
1) Your XYplorer Version (e.g., v28.00.0801)
2) Your Windows Version (e.g., Win 11)
3) Your Screen Scaling Percentage (e.g., 125%).
We strongly recommend adding your Windows Version and Screen Scaling Percentage to the Location field in your Profile or to your Signature. That way, you only have to type them once, and we won't have to search for that vital information.
When attaching an Image, please use the Attachment tab at the bottom of your post and click "Add files".
-
highend
- Posts: 15016
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: Strings and Numbers Comparison Errors
Yes, seems to be a bug. The help file states that these comparisons should always return false, because (at least) only one part is numeric).
One of my scripts helped you out? Please donate via Paypal
-
TheQwerty
- Posts: 4373
- Joined: 03 Aug 2007 22:30
Re: Strings and Numbers Comparison Errors
In all three cases at least one side of the expression cannot be treated as a number which causes both sides to be treated as strings.
Since the first character on either side is not the same this is equivalent to comparing the string "2" to the strings "3" and "5". Assuming it uses ASCII then that's comparing 50 to 51 and 53 respectively and it is indeed less than those.
Why do you expect these to return false?
-
highend
- Posts: 15016
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: Strings and Numbers Comparison Errors
It's only taking the first character from each string for the comparison, it's not 2 < 3 AND 2 < 5. Apart from that TheQuerty is right, it uses the ascii table (and their decimal value for each character) for the comparison.Since the first character on either side is not the same this is equivalent to comparing the string "2" to the strings "3" and "5". Assuming it uses ASCII then that's comparing 50 to 51 and 53 respectively and it is indeed less than those.
E.g.:
msg ("#" < "!");
which would give the result 0, because 35 is not less than 33.
One of my scripts helped you out? Please donate via Paypal
-
TheQwerty
- Posts: 4373
- Joined: 03 Aug 2007 22:30
Re: Strings and Numbers Comparison Errors
Indeed - I worded it poorly and didn't realize "5" was also the second character in the first expression.highend wrote:It's only taking the first character from each string for the comparison, it's not 2 < 3 AND 2 < 5.Since the first character on either side is not the same this is equivalent to comparing the string "2" to the strings "3" and "5". Assuming it uses ASCII then that's comparing 50 to 51 and 53 respectively and it is indeed less than those.
What I meant was in the first case it's comparing the strings "2" vs. "3" and in the other two it's "2" vs. "5".
Also note that you could use the Compare function instead:
Code: Select all
Echo "2" < "10a"; // true;
Echo Compare("2","10a"); // 1 = "2" is greater.
Echo Compare("2","10a", "n"); // -1 = "10a" is greater. This is a numeric comparison of 2 vs. 10.
Echo Compare("10a", "10b", "n"); // 0 = They are equal.
Echo Compare("10a", "a100000", "n"); // 1 = "10a" is greater. Compared 10 vs. 0.-
Enternal
- Posts: 1175
- Joined: 10 Jan 2012 18:26
Re: Strings and Numbers Comparison Errors
The reason why I expected false is:
Directly from the manual. So I used that as a reference. So I guess it's a bit more complicated than I thought...If you compare two numerical strings, they are compared as integers:
msg ("2" < "10"); // -> true! (both parts are numeric)
msg ("2" < "10a"); // -> false! (only one parts numeric)
msg ("2a" < "10a"); // -> false! (both parts are non-numeric)
-
TheQwerty
- Posts: 4373
- Joined: 03 Aug 2007 22:30
Re: Strings and Numbers Comparison Errors
Well let's try to come up with some improvements Don can make to the help...
Would this be better?
Would this be better?
If you compare two numerical strings, they will be treated as integers.
If either side is a string, both will be treated as strings and compared character-by-character.
msg ("2" < "10" );
= Shows '1' (True)
= Compared number 2 to number 10.
msg ("2" < "10a");
msg ("2a" < "10a");
= Both show '0' (False)
= Compared character "2" to character "1" and stopped.
msg ("2" < "2b");
= Shows '1' (True)
= Compared character "2" to character "2".
= Because the left side is shorter it is considered "smaller".
msg ("2a" < "2b");
= Shows '1' (True)
= Compared character "2" to character "2".
= Then compared character "a" to character "b" and stopped.
-
Enternal
- Posts: 1175
- Joined: 10 Jan 2012 18:26
Re: Strings and Numbers Comparison Errors
Ah yes that would be more clear. Hopefully Don reads this when he comes back from break.
XYplorer Beta Club