Iterating over an array to add to TreeView list? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
arcylix
Posts: 54
Joined: 27 Sep 2022, 14:43

Iterating over an array to add to TreeView list?

Post by arcylix » 30 May 2023, 14:16

After a successful venture with writing my first GUI project, I am already working on a second one, and this one is a bit more involved, I think.

I have an xml file structured like the following:

Code: Select all

<triggers>
<trigger enabled="y" match="Some lines of text" sequence="100" regexp="n" ... ></trigger>
</triggers>

<aliases>
<alias enabled="y" match="Some more lines of text" sequence="100" regexp="n" ...></aliases>
and so forth. I am unable to determine since I do not know the proper way to display the array, but I 'assume' that I have the array being populated correctly, using the following code:

Code: Select all

ParseFile(file) {
    ; Read the file contents
    fileContent := FileRead(file)
    currentElement := ""

    ; Prepare an empty array to store the elements
    elements := Array()

    ; Loop over each line in the file content
    for each, line in StrSplit(fileContent, "`n", "`r") {
        ; If the line contains the start of an element, create a new dictionary
        if (RegExMatch(line, "i)<(trigger|alias|timer|variable)", &elementName)) {
            currentElement := {type: elementName[1]}  ; the '1' after elementName gets the first capture group
            elements.Push(currentElement)
        }
        ; If the line contains an attribute, add it to the current element
        if !currentElement = "" {
         if (RegExMatch(line, "i)(\w+)=`"(.*?)`"", &attribute)) {
               currentElement[attribute[1]] := attribute[2]
         }
      }
        ; If the line contains the end of an element, stop adding attributes to it
        if (RegExMatch(line, "i)</(trigger|alias|timer|variable)>")) {
            currentElement := ""
        }
    }
}
What I am hoping to do, as per the topic line, is iterate through the Array and add them as Parent/Child/SubChild elements in the following manner:

Code: Select all

Triggers -
	"Some line of text" -
		enabled = "y"
		sequence = "100"
		regexp = "n"
Aliases -
	"Some more lines of text" -
		enabled ="y"
		sequence = "100"
		regexp = "n"
Is this possible? If so, how would I go about doing this? If not, what would be a potentially better way of doing this?

teadrinker
Posts: 4311
Joined: 29 Mar 2015, 09:41
Contact:

Re: Iterating over an array to add to TreeView list?  Topic is solved

Post by teadrinker » 30 May 2023, 15:34

I'd use this way:

Code: Select all

xml := '
(
    <triggers>
    <trigger enabled="y" match="Some lines of text" sequence="100" regexp="n" ... ></trigger>
    </triggers>
    
    <aliases>
    <alias enabled="y" match="Some more lines of text" sequence="100" regexp="n" ...></aliases>
)'
document := ComObject('HTMLFILE')
document.write('<meta http-equiv="X-UA-Compatible" content="IE=9">')
document.write(xml)

triggers := document.querySelectorAll('trigger')
Loop triggers.length {
    trigger := triggers.%A_Index - 1%
    MsgBox trigger.getAttribute('match') . '`n' . trigger.getAttribute('sequence')
}

aliases := document.querySelectorAll('alias')
Loop aliases.length {
    alias := aliases.%A_Index - 1%
    MsgBox alias.getAttribute('match') . '`n' . alias.getAttribute('sequence')
}

arcylix
Posts: 54
Joined: 27 Sep 2022, 14:43

Re: Iterating over an array to add to TreeView list?

Post by arcylix » 30 May 2023, 18:23

@teadrinker

Awesome! Learned something new today, and was able to quickly adapt it to my purpose! Thank you!

teadrinker
Posts: 4311
Joined: 29 Mar 2015, 09:41
Contact:

Re: Iterating over an array to add to TreeView list?

Post by teadrinker » 30 May 2023, 21:07

Glad to hear it, good luck learning AHK!

Post Reply

Return to “Ask for Help (v2)”