Creating a long asociative array from a literal string?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Nixcalo
Posts: 116
Joined: 06 Feb 2018, 04:24

Creating a long asociative array from a literal string?

Post by Nixcalo » 13 Sep 2021, 00:50

Hi everyone:

I have a lot of strings I need to perform substitutions on. So, instead of having like 20 lines such as

Code: Select all

Target:=StrReplace(Target, "retire", "quite")
Target:=StrReplace(Target, "entrada", "admisión")
Target:=StrReplace(Target, "arnés", "mazo de cables")
Target:=StrReplace(Target, "tornillo", "perno")
Target:=StrReplace(Target, "ntos de tubos", "ntos de tubo")
Target:=StrReplace(Target, "con un par de apriete", "a un par de")
I have thought that it would be much easier to create a variable I can easily modify, without as many inverted commas and whatnot, such as

Code: Select all

Replacements = retire:quite
,entrada:admisión
,arnés:mazo de cables
,tornillo:perno,
ntos de tubos:ntos de tubo
,con un par de apriete:a un par
and then perform a couple of RegexReplace or StrReplace to change it to the appropiate associative array syntax so the variable Replacements ends up reading like this

Code: Select all

Replacements:={"retire":"quite"
,"entrada":"admisión"
,"arnés":"mazo de cables"
,"tornillo":"perno"
,"ntos de tubos":"ntos de tubo"
,"con un par de apriete":"a un par de"}
Then it would be as simple as using a For loop For Word1,Word2 in Replacements
Target:=RegexReplace(Target,"\b" . Word1 . "\b",Word2)
to make all the necessary changes to the strings I get as a "Target".

The thing is, after modifying the variable, adding inverted commas and braces in the right places and whatnot, Replacements is NOT CONSIDERED an array, how the heck do I tell autohotkey to consider it as an asociative array and NOT as a literal string???


This is my entire code where you can see the process step by step

Code: Select all

 Replacements =retire:quite,entrada:admisión,arnés:mazo de cables
,tornillo:perno,ntos de tubos:ntos de tubo
,con un par de apriete:a un par

Replacements :=RegexReplace(Replacements , ":",""":""") ; changes : to ":"
Replacements :=RegexReplace(Replacements , ",", """,""") ; changes , to ","
Replacements :=RegexReplace(Replacements ,"(.)\z", "$1""}") ; changes the last letter of the string to letter"}
Replacements :=RegexReplace(Replacements ,"^(.)", "{""$1") ; changes the first letter of the string to {"letter

Array= %Replacements%
MsgBox,Array %Array%
; And here I am stuck because I can't use the For loop because Array is NOT an array
I had the hope that Array would somehow be considered as an asociative array, but I am not able to conjugate Array and Replacement so AHK understands Array is an asociative array and NOT a literal string.

Any help?
User avatar
flyingDman
Posts: 2776
Joined: 29 Sep 2013, 19:01

Re: Creating a long asociative array from a literal string?

Post by flyingDman » 13 Sep 2021, 01:55

try:

Code: Select all

Replacements =retire:quite,entrada:admisión,arnés:mazo de cables
,tornillo:perno,ntos de tubos:ntos de tubo
,con un par de apriete:a un par de

arr := {}
for x,y in strsplit(replacements,",")
		arr[strsplit(y, ":").2] := strsplit(y, ":").1                ; 2...1 or 1...2 ?

msgbox % arr["mazo de cables"]
msgbox % arr["a un par de"]
14.3 & 1.3.7
just me
Posts: 9406
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Creating a long asociative array from a literal string?

Post by just me » 13 Sep 2021, 04:37

If you only want to convert the string into an array to perform the replacements within a loop, the following might be an option:

Code: Select all

; Might have been read from a file:
Replacements := "
(
retire:quite
entrada:admisión
arnés:mazo de cables
tornillo:perno
ntos de tubos:ntos de tubo
con un par de apriete:a un par de
)"

Loop, Parse, Replacements, `n, `r
{
   Words  := StrSplit(A_LoopField, ":")
   MsgBox, % Words[1] . " -> " . Words[2]
   ; Target := RegexReplace(Target, "\b" . Words[1] . "\b", Words[2])
}
Nixcalo
Posts: 116
Joined: 06 Feb 2018, 04:24

Re: Creating a long asociative array from a literal string?

Post by Nixcalo » 19 Sep 2021, 00:05

Both gave me wonderful ideas and solutions. Thank you!
Post Reply

Return to “Ask for Help (v1)”