[v1] a numeric string pushed as a number after an item of the target object being compared with it

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
fjn308
Posts: 2
Joined: 07 Nov 2022, 07:14

[v1] a numeric string pushed as a number after an item of the target object being compared with it

26 Apr 2023, 20:27

[Moderator's note: Topic moved from Bug Reports.]

minimal illustration code:

Code: Select all

numstr := "01"
array := []
array.Push(numstr)
msgbox % array[1]
If (array[1] = numstr)
{}
array.Push(numstr)
msgbox % array[2]
msgbox % array[1]
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: [v1] a numeric string pushed as a number after an item of the target object being compared with it

07 May 2023, 07:37

Please see :arrow: Caching, also note
= wrote: If both inputs are numbers or numeric strings, they are compared numerically
[...]
Note: In AutoHotkey v1, a quoted string (or the result of concatenating with a quoted string) is never considered numeric when used directly in an expression.
You can work around by doing x.push(y "")

Cheers.
User avatar
lmstearn
Posts: 698
Joined: 11 Aug 2016, 02:32
Contact:

Re: [v1] a numeric string pushed as a number after an item of the target object being compared with it

07 May 2023, 08:58

As a matter of interest, from the above revelation, quoted "01" happens to be a plain string, not a numeric string:

Code: Select all

numstr := """01"""
array := []
array.Push(numstr)
if (array[1] == numstr)
{
array.Push(numstr)
msgbox % "numstr " numstr " array[1] " array[1] " array[2] " array[2]
}
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
fjn308
Posts: 2
Joined: 07 Nov 2022, 07:14

Re: [v1] a numeric string pushed as a number after an item of the target object being compared with it

13 May 2023, 11:19

Helgef wrote: Please see :arrow: Caching ...
Thank you! I think I got the point:

Code: Select all

numstr := "01" ; numstr contains only  a string "01" at first
anothernumstr := "2"
array := []
array.Push(numstr) ; "01" is pushed for it is the only choice
If (numstr = anothernumstr) ; but now numstr contains a number 1, too[1]
{}
array.Push(numstr) ; 1 as the preferred one over "01" is pushed[2]
msgbox % array[2] ; 1
msgbox % array[1] ; 01
[1] In effect, a variable can contain both a string and a number simultaneously.
[2] ... but if a variable contains both a number and a string, is it number, or is it a string? This ambiguity causes unexpected behavior in at least two cases: ... Objects don't have the capability to store both a number and a string as a key or value. Since numbers are more memory-efficient, if a variable has both, the number is used.
So it is NOT a bug. My bad :D

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: arrondark, Chunjee, mikeyww and 75 guests