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
Search replace multi word variable
Re: Search replace multi word variable
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
ax hd black white
ax hd black
ax hd
ax
Re: Search replace multi word variable
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.
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.
Re: Search replace multi word variable
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
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
Re: Search replace multi word variable
Just sort the lines by Length https://autohotkey.com/board/topic/2309 ... ne-length/
Re: Search replace multi word variable
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Search replace multi word variable
Hallo,
this rearranges a key value table, descending by the number of spaces in the keys:
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
Re: Search replace multi word variable
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
I have really learnt a lot
I must say this forum never ceases to amaze me!
Keep it and once again many thanx
Re: Search replace multi word variable
@Rohweder
Having trouble understanding your code--could u explain each line starting from ValTa%NoSpace% .= Key "=" Val "`n"
Many really appreciate it
Having trouble understanding your code--could u explain each line starting from ValTa%NoSpace% .= Key "=" Val "`n"
Many really appreciate it
Re: Search replace multi word variable
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.)
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.)
Re: Search replace multi word variable
@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!!
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!!
Who is online
Users browsing this forum: No registered users and 226 guests