Bug: StrSplit Topic is solved

Discuss the future of the AutoHotkey language
rodemp
Posts: 44
Joined: 15 Nov 2016, 00:28

Bug: StrSplit

03 Nov 2018, 02:42

Code: Select all

x:="10,15,25,991,24,25,199"
n:=[], c:=StrSplit(x, ",")
Loop c.Length()
	n[A_Index] := c[A_Index]

MsgBox n[3] "/" n[7] "`n" (n[3] > n[7]) ; 25/199 `n 1
I found that when I decomposed a string with StrSplit, some of the values were causing problems

When n[7] is between 100 and 199 it was a problem.


Testing with ahk v1 did not have this problem

Code: Select all

x:="10,15,25,991,24,25,199"
n:=[], c:=StrSplit(x, ",")
Loop % c.Length()
	n[A_Index] := c[A_Index]

MsgBox % n[3] "/" n[7] "`n" (n[3] > n[7]) ; 25/199 `n 0
User avatar
jeeswg
Posts: 6828
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Bug: StrSplit  Topic is solved

03 Nov 2018, 03:01

In AHK v1, two variables containing numeric-looking strings would be compared as numbers. This has changed in AHK v2, but there was a suggestion that the AHK v1 behaviour will be brought back. (I'm trying to find the link on GitHub.) You can use +0 for the time being to convert strings to numbers.

Code: Select all

q::
var1 := "2"
var2 := "10"
;MsgBox, % var1 < var2 ;AHK v1: 1
MsgBox(var1 < var2) ;AHK v2: 0
return
[EDIT:] Here's the link:
Adding function StrCompare and disabling string compairsion of operators: < <= > >= by HelgeffegleH · Pull Request #107 · Lexikos/AutoHotkey_L · GitHub
https://github.com/Lexikos/AutoHotkey_L/pull/107
Originally I think If (a < b) performed numeric comparison if both a and b were numeric strings, so to force a string comparison one had to do something like If ("Str" a < "Str" b). In one of the alphas I changed it to perform string comparison if a and b are both strings (numeric or not), but in hindsight this was probably a bad idea. It can give contradictory results with mixed types:
I came to the same conclusion in this thread:
numbers/strings in expressions - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 37&t=55905
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
rodemp
Posts: 44
Joined: 15 Nov 2016, 00:28

Re: Bug: StrSplit

03 Nov 2018, 03:23

I did not know it would be a problem in the simple part. Thanks for helping solve the problem.
Ursi
Posts: 18
Joined: 16 Oct 2018, 14:11
GitHub: Ursi

Re: Bug: StrSplit

03 Nov 2018, 17:00

You can also use the Float and Integer functions to convert strings to numbers.
_3D_
Posts: 211
Joined: 29 Jan 2014, 14:40

Re: Bug: StrSplit

25 Feb 2019, 06:30

As jeeswg wrote you compare string to string.
If you need to compare separated elements as integer: (spl[<index>] + 0) <comparison> (spl[<index>] + 0) - it is forced conversion to integer if possible or exception.
AHKv2.0 use the future now.
User avatar
jeeswg
Posts: 6828
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Bug: StrSplit

07 Jun 2019, 16:30

- I'd like to compile a custom version of AHK v2 that uses the old behaviour, i.e. compare anything that looks numeric, numerically, unless both items are forced as strings at the point where they are compared.
- This example demonstrates what changed:

Code: Select all

a := "1", b := "1.0"
MsgBox(a = b) ;0 (newer versions), 1 (older versions, preferable)
MsgBox(A_AhkVersion)
- Does anyone know where the changed code (re. comparison behaviour) is located in the source code?
- Or in which version it changed? (By testing available exes, I know that the newer behaviour was in force from at least v2.0-a038 onwards.)

- Some source code search terms: SYM_LTOE (less than or equal), this_infix_item.
- Perhaps it's the IsPureNumeric function, introduced in this commit:
Revised handling of types (integer/float/string). · Lexikos/[email protected] · GitHub
https://github.com/Lexikos/AutoHotkey_L/commit/275216e20724568ba7afbe6f2cc29297fd162be2
- [EDIT:] Here's a quick fix (although it goes too far, comparing forced strings that look numeric as numeric):

Code: Select all

;quick fix (script_expression.cpp):
;before:
			if (  !(right_is_number && left_is_number)  // i.e. they're not both numeric (or this is SYM_CONCAT).
				|| IS_RELATIONAL_OPERATOR(this_token.symbol) && !right_is_pure_number && !left_is_pure_number  ) // i.e. if both are strings, compare them alphabetically.

;after:
			if (  !(right_is_number && left_is_number)  // i.e. they're not both numeric (or this is SYM_CONCAT).
				|| false) // i.e. if both are strings, compare them alphabetically.
- Now I need info re. AHK v1, how it detects operands that were forced as strings.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 4 guests