Filter out numbers from Clipboard and summarize them Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Filter out numbers from Clipboard and summarize them

17 Nov 2018, 10:17

Hello,

I have this example string:
Car (12), Street (2), Pedestrian (4), Tree (2)

With an AHK script I would like to filter out just the numbers and create the sum, which would be 20 in this case (12+2+4+2).
I've already made some progress:

Code: Select all

F2::
Clipboard := StrReplace(Clipboard, CHR(44), CHR(43)) ; Replace , with +
Clipboard := StrReplace(Clipboard, CHR(40)) ; Delete (
Clipboard := StrReplace(Clipboard, CHR(41)) ; Delete )
Clipboard := StrReplace(Clipboard, CHR(32)) ; Delete Spaces
Clipboard := RegExReplace(Clipboard, "\D+", "+") ; Replace all text+ by +
StringTrimLeft, Clipboard, Clipboard, 1 ; Delete first character from Clipboard (Because it starts with +)
return
After pressing F2 and pasting the Clipboard, the string is:
12+2+4+2

Two questions:
1)
Are all those StrReplaces really necessary or is there an eleganter solution?
This one made a nice first impression:

Code: Select all

F5::
Clipboard := RegExReplace(Clipboard, "\D+") ; Delete everything which is not a number
return
The problem is that the new string is: 12242
In this case AHK would later count 1+2+2+4+2 instead of 12+2+4+2 - so I can't use this way.

2)
How to create a sum anyway?

If I try this, it works:

Code: Select all

F3::
a := 12+2+4+2 ; Create sum out of this
MsgBox, %a% ; It says 20 which is correct
return
But a := Clipboard would not work, because the MessageBox simply shows the exact Clipboard content -> 12+2+4+2

Thanks for any help!

Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Filter out numbers from Clipboard and summarize them  Topic is solved

17 Nov 2018, 10:38

split on the , then RegExMatch for digits and add them together:

Code: Select all

clip := "Car (12), Street (2), Pedestrian (4), Tree (2)"

Loop Parse, % clip, % ","
{
	RegExMatch(A_LoopField, "\d+", match)
	sum += match
}

MsgBox % sum
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Filter out numbers from Clipboard and summarize them

17 Nov 2018, 12:12

Thank you, swagfag... or should I say RegEx god? :clap:

Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Filter out numbers from Clipboard and summarize them

17 Nov 2018, 12:16

Scr1pter wrote:
17 Nov 2018, 12:12
RegEx god
\d+
some god, lol
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Filter out numbers from Clipboard and summarize them

17 Nov 2018, 14:03

Nah, I didn't mean just this thread. ;)
You saved my ass several times when I got stuck with RegEx :)

Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Filter out numbers from Clipboard and summarize them

17 Nov 2018, 17:54

Pretty similar to swagfag's code, I would do it like this:

Code: Select all

vText := "Car (12), Street (2), Pedestrian (4), Tree (2)"
vText := Trim(RegExReplace(vText, "\D+", " "))
vSum := 0
Loop, Parse, % vText, % " "
	vSum += A_LoopField
MsgBox, % vSum
Btw why the use of Chr()? Although I use Chr() sometimes e.g. Chr(34) for double quotes.

Code: Select all

;equivalent:
Clipboard := StrReplace(Clipboard, Chr(40))
Clipboard := StrReplace(Clipboard, "(")
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Filter out numbers from Clipboard and summarize them

17 Nov 2018, 20:54

Hey jeeswg,
Thanks for your code as well.
I'll check it tomorrow.

Regarding the CHR():
I created a script where the Scroll lock key toggles one mode.
This mode has also the feature to convert all chars to CHR.
I press Spacebar, comma, dot, Enter etc. and it types the CHR.

To be honest I don't remember completely what the main reason was (to write and use that script).
Probably for some characters I simply received a syntax error when I wanted to compile the code.

When using CHR, it seems safer.
I just have to update the script so that it adds comments automatically.
(I can't learn all the CHRs.)

Best regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ConnorMcLoud, Google [Bot] and 166 guests