Why my value exchange function fail? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
nnrxin
Posts: 8
Joined: 07 May 2018, 06:02

Why my value exchange function fail?

23 Feb 2019, 11:29

Code: Select all

P:=1
Q:=2
exchange(P,Q)
MsgBox % "P=" P "`nQ=" Q "`nsuccess"

R:=[1,2]
exchange(R[1],R[2])
MsgBox % "R1=" R[1] "`nR2=" R[2] "`nfail"

exchange(ByRef A,ByRef B)
{
	m:=A,A:=B,B:=m
}
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Why my value exchange function fail?  Topic is solved

23 Feb 2019, 11:43

Here's some code. Cheers.

Code: Select all

q:: ;test assigning obj.key to variable
obj := []
obj.1 := "a"
var := obj.1 ;value of obj.1 is stored, not reference to obj.1
obj.1 := "b"
MsgBox, % var ;a ;obj.1's value changed, but var's value didn't change

R := [1, 2]
MsgBox, % "R1=" R.1 "`nR2=" R.2
exchangekeys(R, 1, 2)
MsgBox, % "R1=" R.1 "`nR2=" R.2
return

exchangekeys(o, A, B)
{
	m := o[A], o[A] := o[B], o[B] := m
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Why my value exchange function fail?

23 Feb 2019, 11:55

Btw if the keys contain string values, you can get addresses for the string values. Long shot, but could you get the address of the object, and the key name for the string value, from that address?

Code: Select all

q:: ;test object get key addresses
obj := []
obj.1 := "1"
obj.2 := "2"
obj.3 := 3
obj.4 := 4
MsgBox, % &obj
MsgBox, % &obj.1 ;doesn't work
MsgBox, % &obj.2 ;doesn't work
MsgBox, % obj.GetAddress(1)
MsgBox, % obj.GetAddress(2)
MsgBox, % obj.GetAddress(3) ;doesn't work (because not string values)
MsgBox, % obj.GetAddress(4) ;doesn't work (because not string values)
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
nnrxin
Posts: 8
Joined: 07 May 2018, 06:02

Re: Why my value exchange function fail?

24 Feb 2019, 07:33

jeeswg wrote:
23 Feb 2019, 11:43
Here's some code. Cheers.

Code: Select all

q:: ;test assigning obj.key to variable
obj := []
obj.1 := "a"
var := obj.1 ;value of obj.1 is stored, not reference to obj.1
obj.1 := "b"
MsgBox, % var ;a ;obj.1's value changed, but var's value didn't change

R := [1, 2]
MsgBox, % "R1=" R.1 "`nR2=" R.2
exchangekeys(R, 1, 2)
MsgBox, % "R1=" R.1 "`nR2=" R.2
return

exchangekeys(o, A, B)
{
	m := o[A], o[A] := o[B], o[B] := m
}
Thank you! It's very helpful.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, Descolada, Oblomov228 and 169 guests