Ghost variables in an array? (Optimizing code; Kind of embedded variable) Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Renets
Posts: 36
Joined: 12 Jul 2018, 13:11

Ghost variables in an array? (Optimizing code; Kind of embedded variable)

08 Aug 2019, 13:44

So, I have this structure which I am trying to optimize it as much as possible

Code: Select all

global current_step := 1 ; It's values are added in other functions, as well as in step[]. It isn't relevant for this
global step:=[] 

Next_Step(){
    if (current_step = 1) {
        for a, b in myArray["main",step[1]] {
            MsgBox % a "-" b
        } 
    }
    else if (current_step = 2) {
        for a, b in myArray["main",step[1],"secundary",step[2]] {
            MsgBox % a "-" b
        } 
    }
    else if (current_step = 3) {
        for a, b in myArray["main",step[1],"secundary",step[2],"tertiary",step[3]] {
            MsgBox % a "-" b
        } 
    }
}
So what I am achieve, by any way, have this kind of structure

Code: Select all

Next_Step(){
    for a, b in myArray[templateArray[current_step] {
          MsgBox % a "-" b
    }     
}
Where templateArray holds the templates for all the options in order to reduce significantly the lines of codes (because I have more than 3 else if's).

I am thinking on filling the templateArray from the beggining with a structure like this

Code: Select all

templateArray := [ " "main",step[1]" ", " "main",step[1],"secundary",step[2]" ", " "main",step[1],"secundary",step[2],"tertiary",step[3] " ]
I know of the quotes rule (""" to send " and """" to send ""). But even with that applied I coudn't get it runnnig.
A fast way to test if I am close to get where I am trying to be, would be this

Code: Select all

step_value_1:="myValue"

template_one = "main", %step_value_1% ; Alternative one
template_one := """main""" "," """" step_value_1 """" ; Alternative two

for a,b in json[template_one] {
MsgBox % a "-" b
}
But I can't even get it work, any ideas are welcome
Thanks!

Edit: Clarify

Problem: How to pass a variable to myArray that contains a sub commands for a specific myArray value

So instead of doing this

Code: Select all

for a, b in myArray["main",step[1]] {
     MsgBox % a "-" b
} 
I can do this

Code: Select all

template_one = "main", %step[1]% 

for a,b in json[template_one] {
     MsgBox % a "-" b
}

EDIT 2: Clarify
I want to access a value in an nested associative array as in the following structure

Code: Select all

secundary := { keyA: "ValueA"
    , keyA: "ValueA"}

myArray := { secundary: secundary }
I can access "ValueA" like this

Code: Select all

MsgBox %  myArray["secundary", "keyA"] ; This display ValueA in a MsgBox

But I need to have stored the commands

Code: Select all

"secundary","keyA"
in a single variable. Say a variable called "tmp". Then instead of having

Code: Select all

myArray["secundary","keyA"] ; Access ValueA
I need to have

Code: Select all

myArray[tmp] ; Access ValueA

CONCLUSION:

I asked in Reddit as well here Broken Link for safety

And in the end I got 2 solutions

1. From G33kDude Broken Link for safety

Code: Select all

secondary:= { keyA: "ValueA", keyB: "ValueA"}
myArray := { secondary: secondary }
MsgBox, % myArray["secondary", "keyA"] ; This display ValueA
tmp := ["secondary","keyA"]
MsgBox, % myArray[tmp*] ; This display ValueA
2. From just me

Code: Select all

InnerArray := {keyA: "ValueA", keyB: "ValueB"}
OuterArray := {InnerArray: InnerArray}
Tmp := "InnerArray,keyA"
MsgBox, % OuterArray[StrSplit(Tmp, ",")*]
Different approaches but both works as neeeded, thank you!
Last edited by Renets on 09 Aug 2019, 14:11, edited 9 times in total.
Renets
Posts: 36
Joined: 12 Jul 2018, 13:11

Re: Ghost variables in an array? (Optimizing code; Kind of embedded variable)

08 Aug 2019, 14:49

swagfag wrote:
08 Aug 2019, 14:31
so what is ur X problem?
Sorry, I edited post to clarify it better
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Ghost variables in an array? (Optimizing code; Kind of embedded variable)

08 Aug 2019, 16:14

no, i understood the y problem. u want to access a variable number of an object's fields at runtime

Code: Select all

Agents := {"british": [{"name": {"real": "James Bond", "undercover": "Agent 007"}}]}
MsgBox % get(Agents, "british", 1, "name", "undercover")

get(Obj, Properties*) {
	rv := Obj
	for each, property in Properties
		rv := rv[property]
	return rv
}
u are yet to clarify what the x problem is, the answer to which will likely be something along the lines of "i want to do Y, because ___" (fill in the blanks)
Renets
Posts: 36
Joined: 12 Jul 2018, 13:11

Re: Ghost variables in an array? (Optimizing code; Kind of embedded variable)

08 Aug 2019, 19:37

swagfag wrote:
08 Aug 2019, 16:14
u are yet to clarify what the x problem is
I tried again, edited post, see Edit2
Thanks for that example though, it is useful for other things of mine as well
just me
Posts: 9574
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Ghost variables in an array? (Optimizing code; Kind of embedded variable)  Topic is solved

09 Aug 2019, 06:43

Code: Select all

#NoEnv

InnerArray := {keyA: "ValueA", keyB: "ValueB"}

OuterArray := {InnerArray: InnerArray}

Tmp := "InnerArray,keyA"

MsgBox, % OuterArray[StrSplit(Tmp, ",")*]
?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Ghost variables in an array? (Optimizing code; Kind of embedded variable)

09 Aug 2019, 08:50

oh lol... looks like i had inadvertently recreated the star operator
Renets
Posts: 36
Joined: 12 Jul 2018, 13:11

Re: Ghost variables in an array? (Optimizing code; Kind of embedded variable)

09 Aug 2019, 11:55

just me wrote:
09 Aug 2019, 06:43

Code: Select all

#NoEnv

InnerArray := {keyA: "ValueA", keyB: "ValueB"}

OuterArray := {InnerArray: InnerArray}

Tmp := "InnerArray,keyA"

MsgBox, % OuterArray[StrSplit(Tmp, ",")*]
YES! That's exactly what I needed! Thanks!

btw1: Thanks swagfag for helping me clarify it better, didn't know about the XY problem, certainly something that many of us do

btw2: ohh man hi just me!
I want to take a moment ot thank you for your libraries for ListViews, they are soooo helpful
Greetings from Mexico!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 388 guests