- @YoucefHam: Your RegEx and ObjRawGet/ObjRawSet code looks good.
- For 'abc.def' I would probably do the following:
Code: Select all
;AHK v1 (notice 'O)'):
RegExMatch("abc.def", "O)^([^.]+)\.([^.]+)$", oMatch)
MsgBox, % oMatch.1 " " oMatch.2
;AHK v2:
RegExMatch("abc.def", "^([^.]+)\.([^.]+)$", oMatch)
MsgBox(oMatch.1 " " oMatch.2)
- Note: I use 'O)' (object mode), because it's more forwards compatible.
- Here's a prototype, but it's not intended for everyday AutoHotkey use. It can only handle objects if the object is in the global namespace.
Code: Select all
q:: ;test swap
var1 := "1"
var2 := "2"
MsgBox, % var1 " " var2 ;1 2
Swap(var1, var2)
MsgBox, % var1 " " var2 ;2 1
obj := {}
obj.key1 := "1"
obj.key2 := "2"
MsgBox, % obj.key1 " " obj.key2 ;1 2
Swap("obj.key1", "obj.key2")
MsgBox, % obj.key1 " " obj.key2 ;2 1
obj1 := {}
obj2 := {}
obj1.key1 := "1"
obj2.key2 := "2"
MsgBox, % obj1.key1 " " obj2.key2 ;1 2
Swap("obj1.key1", "obj2.key2")
MsgBox, % obj1.key1 " " obj2.key2 ;2 1
obj1 := {}
obj1.key1 := "1"
var2 := "2"
MsgBox, % obj1.key1 " " var2 ;1 2
Swap("obj1.key1", var2)
MsgBox, % obj1.key1 " " var2 ;2 1
obj1 := {}
obj1.key1 := {}
obj1.key1.key1 := "1"
var2 := "2"
MsgBox, % obj1.key1.key1 " " var2 ;1 2
Swap("obj1.key1.key1", var2)
MsgBox, % obj1.key1.key1 " " var2 ;2 1
SwapTest()
SwapTest()
{
MsgBox, % "this swap will fail"
objnew := {}
objnew.key1 := "1"
objnew.key2 := "2"
MsgBox, % objnew.key1 " " objnew.key2 ;1 2
Swap("objnew.key1", "objnew.key2")
MsgBox, % objnew.key1 " " objnew.key2 ;2 1
}
Swap(ByRef var1, ByRef var2)
{
global
local objname, temp, value1, value2, varnum
Loop, 2
{
if IsByRef(var%A_Index%) ;variable
value%A_Index% := var%A_Index%
else ;string
{
temp := StrSplit(var%A_Index%, ".")
objname := temp.RemoveAt(1)
value%A_Index% := %objname%[temp*]
}
}
Loop, 2
{
varnum := (A_Index = 1) ? 2 : 1
if IsByRef(var%A_Index%) ;variable
var%A_Index% := value%varnum%
else ;string
{
temp := StrSplit(var%A_Index%, ".")
objname := temp.RemoveAt(1)
%objname%[temp*] := value%varnum%
}
}
}
- @just me: I'm concerned both about how and why you made your helpful(?) point/emoji.
- There's no real argument to be against the functionality per se.
- Whether you want it for AHK or not is another matter.
- What I'm really interested in is parallels in other languages. And the desire for the functionality described, or for workarounds. The general principle is this: avoiding writing multiple functions, by forcing an object key into a function, the *key* rather than its value.
- The most interesting idea might be: a unary operator, that would output the key's value as a temporary ByRef variable, and then retrieve that variable's value, and store it in the key.