Convert text to array

Get help with using AutoHotkey and its commands and hotkeys
f1ster
Posts: 16
Joined: 12 Feb 2020, 01:52

Convert text to array

21 Oct 2020, 08:06

I have the text variable:

Code: Select all

var:="{key1: {a:[1,0], b:[1,0]}, key2: [1,0], key3: [1,0]}"
How can I convert it to array?
Last edited by f1ster on 21 Oct 2020, 09:12, edited 1 time in total.
User avatar
boiler
Posts: 6639
Joined: 21 Dec 2014, 02:44

Re: Convert text to array

21 Oct 2020, 08:33

You can write it to the script file itself and Reload it:

Code: Select all

var:="{key1: {a:[1,0], b:[1,0]}, key2: [1,0], key3: [1,0]}"

if !myarray {
	FileRead, ScriptText, myscript.ahk
	FileDelete, myscript.ahk
	FileAppend, % "myarray := " var "`n" ScriptText, myscript.ahk
	MsgBox, about to reload ; to prevent potential runaway script
	Reload
}

MsgBox, % myarray.key1.a.1

By the way, you had a typo in your string: var:="{key1: {a:[1,0], b:{1,0]}, key2: [1,0], key3: [1,0]}"
(should be [)

Of course, you might not want any permanent changes to your script so it can work dynamically when a new text string is supplied (whether read from a file or whatever), so you would need your script to also remove the temporary additions to the script, which wouldn't be hard to do following a similar approach.
f1ster
Posts: 16
Joined: 12 Feb 2020, 01:52

Re: Convert text to array

21 Oct 2020, 09:21

It is so crazy to add content of the variable to the text of the script already containing it O_o
I am using StrSplit, but want to know more wise way.
User avatar
boiler
Posts: 6639
Joined: 21 Dec 2014, 02:44

Re: Convert text to array

21 Oct 2020, 09:41

Using StrSplit or similar methods is difficult unless you already know the structure of the object definition. Does your approach really work for the general case or just for when you know how the object is structured and what kind of contents the elements contain?
f1ster
Posts: 16
Joined: 12 Feb 2020, 01:52

Re: Convert text to array

21 Oct 2020, 10:13

"StrSplit" and "Loop, parse" it is hard and I hope not the best way.
User avatar
boiler
Posts: 6639
Joined: 21 Dec 2014, 02:44

Re: Convert text to array

21 Oct 2020, 10:23

That’s why I suggested dynamically rewriting the code so you can take advantage of the AHK interpreter doing all the work and ensuring it is done correctly. It would be a Herculean task to write a parser to take any string and create the same object that would be defined as if it were an actual object definition expression.
User avatar
Chunjee
Posts: 747
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: Convert text to array

21 Oct 2020, 10:59

If the string was instead "{"key1":{"a":[1,0],"b":[1,0]},"key2":[1,0],"key3":[1,0]}" you could use one of the ready made json parsers.
teadrinker
Posts: 2059
Joined: 29 Mar 2015, 09:41
Contact:

Re: Convert text to array

21 Oct 2020, 11:09

Code: Select all

var:="{key1: {a:[1,0], b:[1,0]}, key2: [1,0], key3: [1,0]}"

obj := JsonToAHK(var)
MsgBox, % obj.key2.1

JsonToAHK(json, rec := false) {
   static doc := ComObjCreate("htmlfile")
         , __ := doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
         , JS := doc.parentWindow
   if !rec
      obj := %A_ThisFunc%(JS.eval("(" . json . ")"), true)
   else if !IsObject(json)
      obj := json
   else if JS.Object.prototype.toString.call(json) == "[object Array]" {
      obj := []
      Loop % json.length
         obj.Push( %A_ThisFunc%(json[A_Index - 1], true) )
   }
   else {
      obj := {}
      keys := JS.Object.keys(json)
      Loop % keys.length {
         k := keys[A_Index - 1]
         obj[k] := %A_ThisFunc%(json[k], true)
      }
   }
   Return obj
}
f1ster
Posts: 16
Joined: 12 Feb 2020, 01:52

Re: Convert text to array

21 Oct 2020, 12:13

Wow! Pefect!

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, BionicRiddle, Google [Bot], keylo, reklamescobar, thundermet and 56 guests