Page 1 of 1

How to easily calculate the sum of digits?

Posted: Thu Jan 10, 2019 12:12 pm
by Debugger
How to easily calculate the sum of digits in any text without having to add "+" and "=" all the time and paste it into the calculator.
Numbers in lines or after a comma.

Sample 1:
1
56
11
9
37
78
80


Sample 2:
05,07,08,12,14,19,21,29,30,35,43,49,50,61,64,68,70,73,74,80

Re: How to easily calculate the sum of digits?

Posted: Thu Jan 10, 2019 3:54 pm
by therube
addup.bat:

Code: Select all

gawk "{ sum += $1 } END { print sum }" %1 %2 %3
@echo off

pause
:END
ECHO DONE

Code: Select all

C:\BIN>addup

C:\BIN>gawk "{ sum += $1 } END { print sum }"
1
2
3

^Z
6
Press any key to continue . . .
DONE

C:\BIN>
For the comma, you could do something like:

Code: Select all

sed -e  s/,/\n/g  <  setofcommanumbers  |  addup

Code: Select all

C:\TMP\>sed -e s/,/\n/g  < setofcommanumbers | addup

C:\TMP\>gawk "{ sum += $1 } END { print sum }"
812
Press any key to continue . . .
DONE

C:\TMP\>
Or you could have your gawk do a running total...

Gawk for Windows

Re: How to easily calculate the sum of digits?

Posted: Thu Jan 10, 2019 3:58 pm
by therube
This was my original script, running under Xenix.
(It worked MUCH better then what I was able to accomplish under DOS.)

Code: Select all

#
#       @(#) addup.sh 1.0 90/07/19
#
#       Copyright (C) therube, 1990
#       Adds up a column (default=last) of numbers in a file.
#       95/05/16 updated to allow (999) negative style numbers.

case $1 in
-[0-9])
        COLUMN=`echo $1 | tr -d -`
        shift
;;
*)
        COLUMN="NF"
;;
esac
echo "Adding up column .. $COLUMN .. of file(s) .. $*"

nawk  ' OFMT="%.2f"                                       # 1 "%12.2f"
        { x = '$COLUMN'                                   # 2
          neg = index($x, "$")                            # 3
          if (neg > 0) X = gsub("\\$", "", $x)
          neg = index($x, ",")                            # 4
          if (neg > 1) X = gsub(",", "", $x)
          neg = index($x, "(")                            # 8 neg (123 & change
          if (neg > 0) X = gsub("\\(", "", $x)
          if (neg > 0) $x = (-1 * $x)                     # it to "-123.00"
          neg = index($x, "-")                            # 5
          if (neg > 1) $x = (-1 * $x)                     # 6
          t += $x                                         # 7
          print "x is <<<", $x+0, ">>> running balance:", t
        } ' $*

# 1.  set numeric format to eliminate rounding errors
# 1.1 had to reset numeric format from 12.2f to .2f 95/05/16
#     when a computed number is assigned to a variable ( $x = (-1 * $x) )
#     it causes $x to use the OFMT so -1.23 = "________-1.23" vs "-1.23"
#     and that causes my #5 (negative check) to not work correctly because
#     the index returns a number >1 and to the neg neg than becomes a positive
#     this only occurs if the number happened to b a "(" neg number
# 2.  find the field we want to add up (comes from the shell or defaults
#     to the last field "NF") in the file
# 3.  check for a dollar sign ($) in the number - if there get rid of it
#     so we may add it correctly - $12 $1$2 $1$2$ $$1$$2$$ all = 12
# 4.  check for a comma (,) in the number - if there get rid of it so we
#     may add it correctly - 1,2 12, 1,,2 1,,2,, all = 12   (,12=0)
# 5.  check for negative numbers
# 6.  if x is a negative number in the form 999- "make" it a recognized
#     number like -999 - if x is a negative number like -999 already
#     the test fails (y is not >1) and this "true" negative is not made
#     positive
# 7.  accumulate the total
# 8.  if x is a negative number in the form (999) "make it a recognized
#     number like -999
# * Note that a (-9) (neg neg number) returns a postive
# * Mite not work rite with all forms of all numbers using $-,+. etc. *
Not only could you run this from the command-line, but you could also call it from within a text editor (like Vim).
(You can do similar with what's in the 1st. post, but it it nowhere near as clean, as nice.)

Re: How to easily calculate the sum of digits?

Posted: Thu Jan 10, 2019 4:10 pm
by therube
Eval calculator (command line version)

With that, you would have to add + to the numbers, on a single line.

Code: Select all

C:\TMP\>eval < abunchofnumbers
>> +05 +07 +08 +12 +14 +19 +21 +29 +30 +35 +43 +49 +50 +61 +64 +68 +70 +73 +74 +80
ans[0]= 812

C:\TMP\>
abunchofnumbers:

Code: Select all

+05 +07 +08 +12 +14 +19 +21 +29 +30 +35 +43 +49 +50 +61 +64 +68 +70 +73 +74 +80

Re: How to easily calculate the sum of digits?

Posted: Thu Jan 10, 2019 4:39 pm
by Debugger
,

Re: How to easily calculate the sum of digits?

Posted: Thu Jan 10, 2019 6:06 pm
by vanisk
but useless for me
Here we ago again with the "attitude". Don't you think its rude to reply like this to someone who is trying to help you in gaining knowledge.

Please don't try to take the easy way out for all the problems.

Each and everyone's issue/problem/requirement is different than others. So there won't be any ready-made solution for all. I'm dying to say this for quite a looong time.

Further, you should realise now that many of the forum members are avoiding your threads, only because of the attitude.

[I apologise for this outburst, but someone has to say it]

[Note: If any moderators/admin feel this post should be deleted, pls feel free to delete this]

Re: How to easily calculate the sum of digits?

Posted: Thu Jan 10, 2019 6:33 pm
by vanisk
2019-01-11 21_43_46-SumNos - v1.2 - VanisK.jpg

I'm not a very good programmer, but i tried.

https://www120.zippyshare.com/v/IeiQ6iHo/file.html
SumNos v1.2.zip
[Edit: As there were some issues with previous one, updated the image and links]

Re: How to easily calculate the sum of digits?

Posted: Thu Jan 10, 2019 7:52 pm
by Debugger
vanisk - Perfectly, that's what I wanted, for a simple and quick calculation. Many thanks! :mrgreen:

Re: How to easily calculate the sum of digits?

Posted: Fri Jan 11, 2019 12:58 am
by SuperDude
Debugger wrote:
Thu Jan 10, 2019 4:39 pm
but this executable file can not be found on the disk, I do not even know where to look and where it was saved.
If only there was some search program that could find your file... :-)

Re: How to easily calculate the sum of digits?

Posted: Fri Jan 11, 2019 4:52 pm
by NotNull
SuperDude wrote:
Fri Jan 11, 2019 12:58 am
If only there was some search program that could find your file... :-)
:lol: That sounds extremely useful! If you ever find such a tool, please let me know. That would make everything so much easier!


BTW: I am one of those persons who stopped reading @Debugger's posts - a long time ago- as he/she is too unfriendly to my liking to people who are trying to help him by spending their time, skills and knowledge.
At least, that's how it was a couple of months ago.
Please let me know if that changes (for the good, of course :) )

Re: How to easily calculate the sum of digits?

Posted: Fri Jan 11, 2019 5:14 pm
by ovg
NotNull wrote:
Fri Jan 11, 2019 4:52 pm
Please let me know if that changes (for the good, of course :) )
It's impossible, no chance. The same things at https://ghisler.ch/board (makinero vs Total Commander)

Re: How to easily calculate the sum of digits?

Posted: Fri Jan 11, 2019 5:31 pm
by Debugger
ovg - Debugger and Makinero are two completely different people, but at the request of the user "Makinero" I publish topics on various forums in order to solve the issue or problem as soon as possible.
Это смешно, забавно, весело :lol:

NotNull - what is the end for me enough of the calculated requirements
I will give you a dot with an exclamation mark ...now I am lying among the infinite

vanisk - I try not to make life difficult for others. I do not offend anyone, I do not provoke anything, I do not assume controversial threads. Was the appointment of a troll to me as a result of ordinary antipathy to me?

Re: How to easily calculate the sum of digits?

Posted: Fri Jan 11, 2019 5:42 pm
by vanisk
Debugger wrote:
Fri Jan 11, 2019 5:31 pm
vanisk - I try not to make life difficult for others.
I ALWAYS try to make life of others a little bit easier.
I do not offend anyone, I do not provoke anything, I do not assume controversial threads. Was the appointment of a troll to me as a result of ordinary antipathy to me?
I didn't quite understand the statement. However, what we are trying to say is just give others some respect (for the time they take to answer you and for trying to help).

Finally, i have no intention of offending/hurting anyone. If my comment did offend anyone, i apologize.

"To Err is Human"

Re: How to easily calculate the sum of digits?

Posted: Fri Jan 11, 2019 7:30 pm
by Debugger
@SuperDude - Finally! ;) I found an amateur program in my collections. Written once by a friend, especially for me. It's called "Sumator." For example, with 20 numbers, it removes duplicates and shows numbers from 1 to 80.

Image

Re: How to easily calculate the sum of digits?

Posted: Fri Jan 11, 2019 8:26 pm
by NotNull
Debugger wrote:
Fri Jan 11, 2019 5:31 pm
NotNull - what is the end for me enough of the calculated requirements
I will give you a dot with an exclamation mark ...now I am lying among the infinite
I have a couple of things to say, but a public forum is not the place for that. Will send you a PM.