How Do I Sum the Values of a Specific Variable

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AlFlo
Posts: 363
Joined: 29 Nov 2021, 21:46

How Do I Sum the Values of a Specific Variable

28 Feb 2024, 13:52

This is embarrassing, because I think this should be easy ... but I'm striking out.

I'm just trying to add up the numbers in line 18 of my text files titled "Clip Box.txt" which I have in each of my client files. For example, for one client, the value in line 18 might be $5,000 and in another it might be $3,000. So I want the script to total those numbers to give me the value of $8,000.

Here's my attempt (which isn't working):

Code: Select all

#SingleInstance Force 
lst := ""
Total := "0" 
var18 := "0" 

Loop, files, C:\Clients\*, R
	If (A_LoopFileName = "Clip Box.txt")
		{
                ++count
		FileReadLine, var18, % A_LoopFileFullPath, 18
               Total+=var18 ; THIS IS THE PART WHICH ISN'T WORKING
		lst .= var18 "|"
		}
NumRows = %count%

sort, lst, D|

Gui, Font, s11

Gui, Add, ListBox, x11 w225 r%NumRows% vvar18, % lst  ; THIS DISPLAYS ALL OF THE VALUES FOR VAR18 IN A LISTBOX, AND IT WORKS FINE
Gui, Add, Button, x11 w200, Done
Gui, Show

Return

ButtonDone:
    Gui, Submit, Hide

MsgBox Total Retainer: $%Total% ; THIS GIVES ME AN INCORRECT VALUE (WAY LOWER THAN THE CORRECT SUM OF NUMBERS IN VAR18)

Return
What am I doing wrong?
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: How Do I Sum the Values of a Specific Variable

28 Feb 2024, 14:57

Remove the dollar signs and commas before you add the numbers.
AlFlo
Posts: 363
Joined: 29 Nov 2021, 21:46

Re: How Do I Sum the Values of a Specific Variable

28 Feb 2024, 15:09

Thank you, mikeyww!!

In my actual script, I did try to StringReplace out the dollar signs and commas. My MISTAKE was I used

Code: Select all

',
instead of

Code: Select all

`,
D'oh!
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: How Do I Sum the Values of a Specific Variable

28 Feb 2024, 15:26

I do not see that in your script, but I would avoid using deprecated commands. StrReplace is a function that would be able to do that.
AlFlo
Posts: 363
Joined: 29 Nov 2021, 21:46

Re: How Do I Sum the Values of a Specific Variable

28 Feb 2024, 15:38

Thank you, Mikeyww.

A question about AHK etiquette: I usually only post the parts of my scripts which are central to the question I'm asking (although in this case I was wrong), and try to edit out of the posted version all of the other parts of the script to make it less time-consuming for folks to go through my script.

Should I do that?

Or - as in this case - did I make it harder for people trying to help me by excluding relevant parts of the script?
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: How Do I Sum the Values of a Specific Variable

28 Feb 2024, 16:00

Good to consider. My advice is to post a short script that the forum reader can run to demonstrate the problem that you are experiencing.
User avatar
Chunjee
Posts: 1500
Joined: 18 Apr 2014, 19:05
Contact:

Re: How Do I Sum the Values of a Specific Variable

28 Feb 2024, 16:45

Just for fun:

Code: Select all

A := new biga() ; requires https://github.com/biga-ahk/biga.ahk

myNumbers := ["$5,000", "$3,000"]
msgbox, % A.sum(A.map(myNumbers, A.parseInt))
; => 8000

msgbox, % A.sumBy(myNumbers, A.parseInt)
; => 8000
https://biga-ahk.github.io/biga.ahk/#/?id=parseint
https://biga-ahk.github.io/biga.ahk/#/?id=sum
https://biga-ahk.github.io/biga.ahk/#/?id=map
https://biga-ahk.github.io/biga.ahk/#/?id=sumby
Last edited by Chunjee on 28 Feb 2024, 17:30, edited 1 time in total.
AlFlo
Posts: 363
Joined: 29 Nov 2021, 21:46

Re: How Do I Sum the Values of a Specific Variable

28 Feb 2024, 17:22

mikeyww wrote:
28 Feb 2024, 16:00
Good to consider. My advice is to post a short script that the forum reader can run to demonstrate the problem that you are experiencing.
Thanks, Mikeyww. My mistake on this post is that I had 3 stringreplaces, since line 18 of my text files actually reads: "Retainer Balance: $x,xxx.xx", and I thought that the script to remove extraneous information like "Retainer Balance" was TMI for the posters.

I'll work on a better balance in the future.
AlFlo
Posts: 363
Joined: 29 Nov 2021, 21:46

Re: How Do I Sum the Values of a Specific Variable

28 Feb 2024, 17:23

Chunjee wrote:
28 Feb 2024, 16:45
Just for fun:

Code: Select all

A := new biga() ; requires https://github.com/biga-ahk/biga.ahk

myNumbers := ["$5,000", "$3,000"]
msgbox, % A.sum(A.map(myNumbers, A.parseInt))
; => 8000

msgbox, % A.sumBy(myNumbers, A.parseInt)
; => 8000
https://biga-ahk.github.io/biga.ahk/#/?id=parseint
https://biga-ahk.github.io/biga.ahk/#/?id=sum
https://biga-ahk.github.io/biga.ahk/#/?id=map
https://biga-ahk.github.io/biga.ahk/#/?id=sumby
Thanks, Chunjee. I'll give it a try.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Mateusz53, MrDoge, peter_ahk and 338 guests