- Re. displaying variables as strings/numbers.
- The 'if' line appears to have no effect on the variable vNum, when you do FileAppend vNum, it always outputs a string.
- What does appear to have an effect, on what FileAppend outputs, is how you store the variable in the array. E.g. when you do FileAppend [vNum].1 or FileAppend["" vNum].1.
- E.g. here I store the variable raw and as a string in 2 different keys:
oArray := [vNum, "" vNum]
Code: Select all
q:: ;check if 'if vNum' affects the output
vPath := A_Desktop "\z " A_Now ".txt"
vNum := "123.123"
FileAppend, % vNum "`r`n", % "*" vPath, UTF-8 ;123.123
FileAppend, % vNum "`r`n", % "*" vPath, UTF-8 ;123.123
if vNum
FileAppend, % vNum "`r`n", % "*" vPath, UTF-8 ;123.123
oArray := [vNum, "" vNum]
FileAppend, % oArray.1 "`r`n", % "*" vPath, UTF-8 ;123.123000
FileAppend, % oArray.2 "`r`n", % "*" vPath, UTF-8 ;123.123
Run, % vPath
return
- [EDIT:] This slightly amended version of the test code suggests that [vNum] initially stores a string, but following 'if vNum', it stores a float. This suggests that 'if vNum' affects the variable in some way, however, when you do FileAppend vNum, it consistently outputs a string.
Code: Select all
q:: ;check if 'if vNum' affects the output
vPath := A_Desktop "\z " A_Now ".txt"
vNum := "123.123"
FileAppend, % vNum "`r`n", % "*" vPath, UTF-8 ;123.123
FileAppend, % vNum "`r`n", % "*" vPath, UTF-8 ;123.123
FileAppend, % "`r`n", % "*" vPath, UTF-8
oArray := [vNum, "" vNum]
FileAppend, % oArray.1 "`r`n", % "*" vPath, UTF-8 ;123.123
FileAppend, % oArray.2 "`r`n", % "*" vPath, UTF-8 ;123.123
FileAppend, % "`r`n", % "*" vPath, UTF-8
if vNum
Sleep, 0
oArray := [vNum, "" vNum]
FileAppend, % oArray.1 "`r`n", % "*" vPath, UTF-8 ;123.123000
FileAppend, % oArray.2 "`r`n", % "*" vPath, UTF-8 ;123.123
FileAppend, % "`r`n", % "*" vPath, UTF-8
Run, % vPath
return
- [EDIT:] Here is a more complete test script:
Code: Select all
q:: ;check if 'if vNum' affects the output
vPath := A_Desktop "\z " A_Now ".txt"
Loop, 4
{
vIndex := A_Index
Loop, 2
{
if (A_Index = 1)
vNum := "123.123"
;vNum := "000123"
else
{
if vNum
Sleep, 0
}
(vIndex = 2) && (oArray := [vNum])
(vIndex = 3) && (oArray := ["" vNum])
(vIndex = 4) && (oArray := [0 + vNum])
if (vIndex = 1)
FileAppend, % vNum "`r`n", % "*" vPath, UTF-8
else
FileAppend, % oArray.1 "`r`n", % "*" vPath, UTF-8
}
FileAppend, % "`r`n", % "*" vPath, UTF-8
}
Run, % vPath
return
;==================================================
;results:
;vNum:
;123.123 ;string
;123.123 ;string
;[vNum]
;a discrepancy:
;the use of 'if vNum' (a read operation),
;has had a write effect,
;and as a result, a float was stored, not a string
;123.123 ;string
;123.123000 ;float
;["" vNum]
;123.123 ;string
;123.123 ;string
;[0 + vNum]
;123.123000 ;float
;123.123000 ;float
;==================================================
- [EDIT:] The explanation is here:
Concepts and Conventions | AutoHotkey
https://autohotkey.com/docs/Concepts.htm#caching
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 (except for floating-point values used as keys).
- Mentioned here:
Object.Push is eating initial zeros - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=64932&p=278890#p278890
- vNum contains a string. Doing 'if vNum' requires a string and a numerical comparison. So a number is cached for vNum. There is now both a string and a number associated with vNum. When [vNum] is done, as described above, the number stored, and not the string.