Trying to convert a List

Ask gaming related questions (AHK v1.1 and older)
WOlfen
Posts: 61
Joined: 14 Jan 2018, 16:48

Trying to convert a List

20 Sep 2020, 05:50

Hello.
I have this in my Script.

Code: Select all

List1 = Apple|People|Unity|Music|RPGMaker|Car|UnrealEngine|Movie
msgbox, 4096, List1, %List1%

List2=
(Join Comment
Apple = ;stuff for the script
People = ;other stuff for the script
Unity = -screen-width 3440 -screen-height 1440
Music = ;stuff for the script
RPGMaker = "NormalPlay" "ShowTitle" "Window"
Car = ;stuff for the script
UnrealEngine = 
Movie = ;again other stuff for the script
)
convList := StrReplace(List2," =","|")
msgbox, 4096, List2, %convList%
To clarify:
- List1 is not in the script, it is only there to show the example.
- I want that the second Messagebox (List2) shows the content of List2 exactly as the first Messagebox (List1).
- If you try out the script above, you will see that it got rid of the = , but still shows stuff after it. I want to get rid of that.

So to even more clarifying from what i need:
What Messagebox (List2) shows:
[Apple|People|Unity| -screen-width 3440 -screen-height 1440Music|RPGMaker| "NormalPlay" "ShowTitle" "Window"Car|UnrealEngine|Movie|
What Messagebox (List2) should show:
Apple|People|Unity|Music|RPGMaker|Car|UnrealEngine|Movie
Last edited by BoBo on 21 Sep 2020, 04:50, edited 1 time in total.
Reason: Moved to Gaming section. Replaced single line [c][/c]-tags with multiline [code][/code]-tags.
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Trying to convert a List

20 Sep 2020, 06:32

This is one way to do it

Code: Select all

List1 = Apple|People|Unity|Music|RPGMaker|Car|UnrealEngine|Movie
msgbox, 4096, List1, %List1%

List2=
(
Apple = ;stuff for the script
People = ;other stuff for the script
Unity = -screen-width 3440 -screen-height 1440
Music = ;stuff for the script
RPGMaker = "NormalPlay" "ShowTitle" "Window"
Car = ;stuff for the script
UnrealEngine =
Movie = ;again other stuff for the script
)
Loop, parse, list2, `n
	NewList .= StrSplit(a_loopfield, a_space).1 "|"
;convList := StrReplace(List2," =","|")
msgbox, 4096, List2, % RTrim(NewList, "|")
ExitApp
WOlfen
Posts: 61
Joined: 14 Jan 2018, 16:48

Re: Trying to convert a List

20 Sep 2020, 16:05

Wow, thank you so much. This works great.
Now, what if the some of the Words have a "Space" in it?

Code: Select all

List2=
(Join Comment
Apple = ;stuff for the script
People = ;other stuff for the script
Unity 2.5 = -screen-width 3440 -screen-height 1440
Music = ;stuff for the script
RPGMaker = "NormalPlay" "ShowTitle" "Window"
Car = ;stuff for the script
Unreal Engine = 
Movie = ;again other stuff for the script
)
Please notice now "Unreal Engine" and "Unity 2.5" (one has a SPACE in it and the other the additional 2.5).
In the end i would need it like: Apple|People|Unity2_5|Music|RPGMaker|Car|UnrealEngine|Movie
I tried a StrReplace on the output, but this doesn´t seem to work, i don´t know.
Thank you very much again for your help.
Last edited by BoBo on 21 Sep 2020, 04:52, edited 1 time in total.
Reason: Replaced single line [c][/c]-tags with multiline [code][/code]-tags.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Trying to convert a List

20 Sep 2020, 16:25

Hi WOlfen,

Based on AHKStudent's solution; you can use = instead as delimiter when calling StrSplit:

Code: Select all

List1 = Apple|People|Unity|Music|RPGMaker|Car|UnrealEngine|Movie
msgbox, 4096, List1, %List1%

delimiter := "`r`n"

List2=
(Join%delimiter% Comment
Apple = ;stuff for the script
People = ;other stuff for the script
Unity 2.5 = -screen-width 3440 -screen-height 1440
Music = ;stuff for the script
RPGMaker = "NormalPlay" "ShowTitle" "Window"
Car = ;stuff for the script
Unreal Engine =
Movie = ;again other stuff for the script
)
CRLF := (delimiter = "`r`n"), NewList := ""
Loop, parse, list2, % (CRLF) ? "`n" : delimiter, % (CRLF) ? "`r" : delimiter
	NewList .= StrSplit(a_loopfield, "=", A_Space).1 "|"
msgbox, 4096, List2, % RTrim(NewList, "|")
ExitApp
Hope this helps

A_AhkUser
my scripts
WOlfen
Posts: 61
Joined: 14 Jan 2018, 16:48

Re: Trying to convert a List

20 Sep 2020, 17:57

Ah, okay.
Thanks for a second solution. This works too, yeah.
But i think i´ll go for AHKStudent's solution, just because i understand it more. But still thanks for another solution.
Going back to my second question:
Do you maybe know how to get Apple|People|Unity2_5|Music|RPGMaker|Car|UnrealEngine|Movie from this:

Code: Select all

List2=
(Join Comment
Apple = ;stuff for the script
People = ;other stuff for the script
Unity 2.5 = -screen-width 3440 -screen-height 1440
Music = ;stuff for the script
RPGMaker = "NormalPlay" "ShowTitle" "Window"
Car = ;stuff for the script
Unreal Engine =
Movie = ;again other stuff for the script
)
Please notice "Unreal Engine" and "Unity 2.5".
Last edited by BoBo on 21 Sep 2020, 04:52, edited 1 time in total.
Reason: Replaced single line [c][/c]-tags with multiline [code][/code]-tags.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Trying to convert a List

21 Sep 2020, 04:36

Code: Select all

List2=
(Comment
Apple = 									;stuff for the script
People = ;       other stuff for the script
Unity 2.5 = -screen-width 3440 -screen-height 1440
Music =;stuff for the script
RPGMaker = "NormalPlay" "ShowTitle" "Window"
Car =;       stuff for the script
Unreal Engine =
Movie = ;again other stuff for the script
)

Loop, Parse, List2, `n`r
		line .= StrSplit(A_LoopField," =").1 "|"
MsgBox % Trim(line,"|")
MsgBox % StrReplace(Trim(line,"|"),"|","||",,1)			; adding additional '|' after 1st item for AHK DDL 1st item preselection
Tested.

PS. Please use multiline code-tags where appropriate! Thx :)
PPS. Please check AHK's command reference for Continuation Section's 'Join' - command.
WOlfen
Posts: 61
Joined: 14 Jan 2018, 16:48

Re: Trying to convert a List

21 Sep 2020, 05:17

BoBo wrote:
21 Sep 2020, 04:36

Code: Select all

List2=
(Comment
Apple = 									;stuff for the script
People = ;       other stuff for the script
Unity 2.5 = -screen-width 3440 -screen-height 1440
Music =;stuff for the script
RPGMaker = "NormalPlay" "ShowTitle" "Window"
Car =;       stuff for the script
Unreal Engine =
Movie = ;again other stuff for the script
)

Loop, Parse, List2, `n`r
		line .= StrSplit(A_LoopField," =").1 "|"
MsgBox % Trim(line,"|")
MsgBox % StrReplace(Trim(line,"|"),"|","||",,1)			; adding additional '|' after 1st item for AHK DDL 1st item preselection
Very nice, thank you.
Although one last question.
I later on would also need access to the stuff which is written after every "=".
For example the list is like this:
List2=
(Comment
Unity 2.5 = -screen-width 3440 -screen-height 1440
Music = ;stuff for the script
RPGMaker = "NormalPlay" "ShowTitle" "Window"
Unreal Engine =
)

I just tested to get the variable text with msgbox, %RPGMaker%, but it´s empty.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Trying to convert a List

21 Sep 2020, 07:44

You can convert 'List2' into an associative array, but I'd strongly recommend using an Ini-file. That way you can easily extract key and values.
:arrow: IniRead/IniWrite.
WOlfen
Posts: 61
Joined: 14 Jan 2018, 16:48

Re: Trying to convert a List

21 Sep 2020, 08:49

This is not from an ini. This is in the script.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Trying to convert a List

21 Sep 2020, 10:17

This is not from an ini. This is in the script.
Yep. And for unknown reason you embed your key=value in a string (variable AKA 'List2'), only to be forced to re-convert them to working key/variable pairs. That's (IMHO) kinda weird, especially from a maintenance perspective.
WOlfen
Posts: 61
Joined: 14 Jan 2018, 16:48

Re: Trying to convert a List

21 Sep 2020, 11:49

Hmm, i see.
Well, then i should clarify.
Before all of this i had it like this in my script:

EngineList = Apple|People|Unity 2.5|Music|RPGMaker|Car|Unreal Engine|Movie

Apple = ;stuff for the script
People = ;other stuff for the script
Unity 2.5 = -screen-width 3440 -screen-height 1440
Music = ;stuff for the script
RPGMaker = "NormalPlay" "ShowTitle" "Window"
Car = ;stuff for the script
Unreal Engine =
Movie = ;again other stuff for the script

Gui, SetupMenu: Add, DropDownList, x170 y69 w200 vEngine1, %EngineList%

So as you can see, the GUI Dropdownlist uses the EngineList Variable for it´s items.
The problem was, this code i posted is heavily trimmed down, in the real script it´s much bigger (more variables and a very long EngineList) and
i´m adding constantly new items to it.
So i came to the idea to just getting rid of the EngineList = Apple|People|Unity 2.5|Music|RPGMaker|Car|Unreal Engine|Movie line, and instead just using the Variables without the strings for the Dropdownlist.
So to clarify again, what i´m trying to do here:

EngineList =
(
Apple = ;stuff for the script
People = ;other stuff for the script
Unity 2.5 = -screen-width 3440 -screen-height 1440
Music = ;stuff for the script
RPGMaker = "NormalPlay" "ShowTitle" "Window"
Car = ;stuff for the script
Unreal Engine =
Movie = ;again other stuff for the script
)

; Some code here

Gui, SetupMenu: Add, DropDownList, x170 y69 w200 vEngine1, %EngineList%


; Some code here
Here i want that the script converts the "SubVariables" of "EngineList", so it looks like this:
EngineList = Apple|People|Unity 2.5|Music|RPGMaker|Car|Unreal Engine|Movie

The Script at the same time should still allow me to use the "SubVariables", so msgbox, %RPGMaker% would still return "NormalPlay" "ShowTitle" "Window"
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Trying to convert a List

21 Sep 2020, 14:57

WOlfen wrote:
21 Sep 2020, 11:49
(...)
Here i want that the script converts the "SubVariables" of "EngineList", so it looks like this:
EngineList = Apple|People|Unity 2.5|Music|RPGMaker|Car|Unreal Engine|Movie

The Script at the same time should still allow me to use the "SubVariables", so msgbox, %RPGMaker% would still return "NormalPlay" "ShowTitle" "Window"
In this case you might need to build an associative array - here's a commented example:

Code: Select all

EngineList =
(Join`n Comment
Apple = ;stuff for the script
People = ;other stuff for the script
Unity 2.5 = -screen-width 3440 -screen-height 1440
Music = ;stuff for the script
RPGMaker = "NormalPlay" "ShowTitle" "Window"
Car = ;stuff for the script
Unreal Engine = test
Movie = ;again other stuff for the script
)
substrings := StrSplit(EngineList, ["=", "`n"], A_Space)
; separate EngineList into an array of substrings using both the '=' and '`n' (linefeed) as delimiters
; the StrSplit function returns an array (object) of strings - that is: ["Apple", "", ...., "RPGMaker", "NormalPlay" "ShowTitle" "Window"", ..., "Movie", ""]
config := Object(substrings*)
; create an associative array from the array. An associative array is a collection of unique keys and a collection of values, where each key is associated with one value
; the wilcard indicates to the script here:
; 1° that we don't want to pass a reference to the array nor the array as a list of parameters (use its elements as parameters).
; 2° that the number of parameters hence depends on the total number of elements in the array: it is variadic.
; since we passed all substrings' elements as parameters here, keys will be entries such as RPGMaker and values their respective value in the EngineList variable - something like this:
; {"Apple": "",... , "RPGMaker": "NormalPlay"" ""ShowTitle"" ""Window",... , "Movie": ""}
; but without hard-coding it here

; alternatively, you can build config like this (less cryptic):
config := {}
for each, value in substrings
{
	if (mod(a_index, 2)) ; if a_index is odd...
		lastFoundValue:=value ; save the key for the next iteration...
	else config[lastFoundValue] := value ; associate the key to a value
}

keys := ""
for key, value in config
{
	; MsgBox % key "," value
	keys .= "|" key
}
; Some code here
Gui, Add, DropDownList, x170 y69 w200 vEngine1 glabel, % keys
GUI, Show, AutoSize
return

label:
GuiControlGet, selectedItem,, % A_GuiControl
; if (config.hasKey(selectedItem))
	ToolTip % config[selectedItem] ; use the brackets to retrieve an item from the associative array
return
hope this helps

A_AhkUser


[addendum]:
If sometimes an equal sign appears in the value (cmd params) associated with a key (engine), on might need to use something like the following instead:

Code: Select all

config := {}
Loop, parse, EngineList, `n ; retrieves substrings (fields) from EngineList, one at a time, using the linefeed as delimiter
{
	parts := StrSplit(A_LoopField, " = ", A_Space, 2)
	; split each field itself:
	; 1° using the equal sign as delimiter
	; 2° excluding from the beginning and end of each element trailing space(s), if need be
	; 3° returning at most 2 substrings, even if there's more than one equal sign in the field: the remainder of the string being returned in the second substring
	config[ parts.1 ] := parts.2
}
keys := ""
for key, value in config
{
	; MsgBox % key "," value
	keys .= "|" key
}
Gui, Add, DropDownList, x170 y69 w200 vEngine1 glabel, % keys
GUI, Show, AutoSize
return

label:
GuiControlGet, selectedItem,, % A_GuiControl
; if (config.hasKey(selectedItem))
	ToolTip % config[selectedItem] ; use the brackets to retrieve an item from the associative array
return
A_AhkUser
my scripts

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 105 guests