Updating my current script which modifies multiple lines into a single one

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
sneazy-ibo
Posts: 6
Joined: 06 Feb 2022, 09:33
Contact:

Updating my current script which modifies multiple lines into a single one

Post by sneazy-ibo » 16 May 2022, 12:58

My current script copies text like this with a shortcut:

Code: Select all

:WiltedFlower: aetheryxflower ─ 4
:Alcohol: alcohol ─ 3,709
:Ant: ant ─ 11,924
:Apple: apple ─ 15
:ArmpitHair: armpithair ─ 2
and pastes it modified into a single line

Code: Select all

Pls trade 4 aetheryxflower 3 alcohol 11 ant 15 apple 2 armpithair <@id>
As you can see there are already two little problems, the first one is that it copies only the number/s before a comma if one existed instead of ignoring it. The second is that I always need to also copy before hitting the hotkey and start re/start the script, I've thought of modifying the script so that it uses the already selected text instead of the copied one so that I can bind it with a single hotkey.

That is my current script, it would be cool if anyone can also tell me what they used and why exactly, so that I also get better with ahk

Code: Select all

!q::
list =
While pos := RegExMatch(Clipboard, "(\w*) ─ (\d*)", m, pos ? pos + StrLen(m) : 1)
 list .= m2 " " m1 " "
Clipboard := "", Clipboard := "Pls trade " list " <@951737931159187457>"
ClipWait, 0
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
return
Last edited by gregster on 16 May 2022, 13:03, edited 1 time in total.
Reason: Topic moved from 'Tutorials' to 'Ask For Help'.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Updating my current script which modifies multiple lines into a single one

Post by BoBo » 16 May 2022, 23:54

RegEx hates me, so I had to think about a workaround like this… :shh:

Code: Select all

text =
(
:WiltedFlower: aetheryxflower ─ 4
:Alcohol: alcohol ─ 3,709
:Ant: ant ─ 11,924
:Apple: apple ─ 15
:ArmpitHair: armpithair ─ 2
)

Loop, parse, text, `n
   {  str := StrSplit(StrSplit(LTrim(A_LoopField,":"),":").2," ─ ") ; remove leading colon|split at remaining colon|take 2nd item and split it at the hyphen char
      line .= str.2 . A_Space . str.1 . A_Space
   }
MsgBox % "Pls trade " . line . "<@951737931159187457>"

sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Updating my current script which modifies multiple lines into a single one

Post by sofista » 17 May 2022, 08:12

Try the following:

Code: Select all

text := "
(
:WiltedFlower: aetheryxflower ─ 4
:Alcohol: alcohol ─ 3,709
:Ant: ant ─ 11,924
:Apple: apple ─ 15
:ArmpitHair: armpithair ─ 2
)"

list := ""
While pos := RegExMatch(text, "(\w*) ─ ([\d,]*)", m, pos ? pos + StrLen(m) : 1)    ; all numbers and comma included in m2
	list .= RegExReplace(m2, ",") " " m1 " "                                       ; comma removed from m2
MsgBox, % "Pls trade " list " <@951737931159187457>"

; Output: Pls trade 4 aetheryxflower 3709 alcohol 11924 ant 15 apple 2 armpithair  <@951737931159187457>

Post Reply

Return to “Ask for Help (v1)”