Search replace multi word variable

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Search replace multi word variable

22 Jul 2018, 16:44

I have a key value table with a large amount of values
value key table below
ax=1
ax hd =2
ax hd black =3
ax hd black white =4


my text file looks like the following
abc, ax
xccvb, ax hd black
122, ax hd black white
4666454, ax hd

I want the output file to look like the following:

abc, 1
xccvb, 3
122, 4
4666454, 2


The problem using stringreplace is the every occurrence of "ax" is immediately replaced with 2 and so on--I need to read all words into one variable
Hope I have made my problem clear
I think I need a regex which is greedy to solve the problem the regex must contain a variable as I need it to enumerate the value key table
Hope someone can help me out
Many thanx
Guest

Re: Search replace multi word variable

23 Jul 2018, 01:44

You can use StrReplace just fine, just start with the longest string and the shortest as last:

ax hd black white
ax hd black
ax hd
ax
Rohwedder
Posts: 7679
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Search replace multi word variable

23 Jul 2018, 02:06

Hallo,
just rearrange your key value table, descending by the number of spaces in the keys.
The keys with the most spaces will first replaced by the respective value.
smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Re: Search replace multi word variable

23 Jul 2018, 05:12

Many thanx for your answer
Yeah I understand what you are saying but my key file is very large and with no real pattern--- what I showed was only a snippet showing the problem
This solution would not work for values like "axn white, axn green, axn blue" the key would be the same key for all values
I am not so sure how to rearrange the file as per your quote "just rearrange your key value table, descending by the number of spaces in the keys. The keys with the most spaces will first replaced by the respective value."
Any ideas would be great
Thanx
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Search replace multi word variable

23 Jul 2018, 06:47

Sometimes I do things line-by-line instead of via RegEx. In the example you gave it seems that all you need to do is count the spaces after the comma.

Code: Select all

q:: ;count number of spaces after comma
vText := " ;continuation section
(
abc, ax
xccvb, ax hd black
122, ax hd black white
4666454, ax hd
)"

vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2)
Loop, Parse, vText, `n, `r
{
	vPos := InStr(A_LoopField, ",")+1
	StrReplace(SubStr(A_LoopField, vPos), " ",, vCount)
	vOutput .= SubStr(A_LoopField, 1, vPos) vCount "`r`n"
}
Clipboard := vOutput
MsgBox, % "done"
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Rohwedder
Posts: 7679
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Search replace multi word variable

23 Jul 2018, 08:29

Hallo,
this rearranges a key value table, descending by the number of spaces in the keys:

Code: Select all

#SingleInstance, Force
ValTa =
(
ax=1
ax hd =2
ax hd black =3
ax hd black white =4
hd=5
hd black =6
hd black white =7
)
Loop, Parse, ValTa,`n
{
	StringSplit, P, A_LoopField,=
	Key := Trim(P1), Val := Trim(P2)
	StrReplace(Key," ","",NoSpace)
	ValTa%NoSpace% .= Key "=" Val "`n"
	MaxNoSpace := Max(NoSpace,0 MaxNoSpace)
}
ValTa := ValTa0
Loop, %MaxNoSpace%
	ValTa := ValTa%A_Index% ValTa
MsgBox, % ValTa
smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Re: Search replace multi word variable

23 Jul 2018, 14:45

Many thanx to all you guys for your prompt answers
I have really learnt a lot
I must say this forum never ceases to amaze me!
Keep it and once again many thanx
smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Re: Search replace multi word variable

24 Jul 2018, 03:25

@Rohweder
Having trouble understanding your code--could u explain each line starting from ValTa%NoSpace% .= Key "=" Val "`n"
Many really appreciate it
Rohwedder
Posts: 7679
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Search replace multi word variable

24 Jul 2018, 04:44

Hallo,
Just this line!!
(English is harder for me than Autohotkey)
The variables ValTa0, ValTa2, ValTa2 - ValTa%MaxNoSpace% form a Pseudo-Array.
https://autohotkey.com/docs/misc/Arrays.htm#pseudo
In this script line the variable ValTa%NoSpace% gets its number from the space numbers of the key-value lines.
With ValTa%NoSpace% .= Key "=" Val "`n" just a new key=value line will be added to the matching Pseudo-Array-variable!
E.g.: A key-value line has 4 spaces, this line will be added to variable ValTa4.
("`n" is a line break.)
smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Re: Search replace multi word variable

24 Jul 2018, 10:57

@Rohweder
Many thanx now I understand
Your English is fine I wish my German was 1/10 as good as your English!
Have a good evening!!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 226 guests