Sign in to follow this  
Followers 0
maxcronjob

Adding up a column of numbers

14 posts in this topic

I have a textfile that contains a column of numbers. I could've even saved this to a .csv file, but in either case, I just don't know how to go about adding up these numbers. This script could dump out a hundred or more lines.

any suggestions?

thanks!

max

Share this post


Link to post
Share on other sites



I have a textfile that contains a column of numbers. I could've even saved this to a .csv file, but in either case, I just don't know how to go about adding up these numbers. This script could dump out a hundred or more lines.

any suggestions?

thanks!

max

You'll need to supply the format of the file to begin with and a coding attempt at trying to solve this issue would be usefull also.


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

Requires beta (or you can change the fileread line and the "+=" line)

$total = 0
$var = StringSplit(FileRead("num.txt"), @CRLF)
For $i = 1 To $var[0]
    $total += $var[$i]
Next
MsgBox(0, 'total', $total)
Assumes each line of the text file is terminated with CRLF.


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Requires beta (or you can change the fileread line and the "+=" line)

$total = 0
$var = StringSplit(FileRead("num.txt"), @CRLF)
For $i = 1 To $var[0]
    $total += $var[$i]
Next
MsgBox(0, 'total', $total)
Assumes each line of the text file is terminated with CRLF.

I get an AutoIt Error:

$var = StringSplit(FileRead("myfile.txt"), @CRLF)

$var = StringSplit(^ ERROR

Error: Incorrect number of parameters in function call.

I'm using Beta 3.1.1.74

-max

Share this post


Link to post
Share on other sites

works with v3.1.1

$total = 0
$var = StringSplit(FileRead("num.txt", FileGetSize("num.txt")), @CRLF)
For $i = 1 To $var[0]
    $total = $total + $var[$i]
Next
MsgBox(0, 'total', $total)
the other code was tested with v3.1.1.108


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

works with v3.1.1

$total = 0
$var = StringSplit(FileRead("num.txt", FileGetSize("num.txt")), @CRLF)
For $i = 1 To $var[0]
    $total = $total + $var[$i]
Next
MsgBox(0, 'total', $total)
the other code was tested with v3.1.1.108

Not sure why, but it's only reading the first line of the textfile and using that as a 'total', but if I run my main script again, it'll sum up all the lines and include the first line again in the sum too.

Now, I'm reading in my values from an array like this, so I'm not using @CRLF. If I use @CRLF in the _ArrayToString, I don't seem to get ANYTHING dumped out into the .txt file:

$outputfilename1 = _ArrayToString($aAttrValue, @CR, 3, 3)

The function and the textfile are both below:

Here's the function:

**********************

Func Sum_TestCases1 ()

$outputfile1 = FileOpen(@ScriptDir & "\Scorecard_Testcases1.csv", 0)

sleep(1000)

FileClose($outputfile1)

$outputfile1 = FileOpen(@ScriptDir & "\Scorecard_Testcases1.csv", 0)

sleep(1000)

$total = 0

$var = StringSplit(FileRead("C:\A_working\sprint3\Scorecard_Testcases1.txt", FileGetSize("C:\A_working\sprint3\Scorecard_Testcases.txt")), @CR)

;FileWriteLine($Debuglog, "$var = " & $var)

For $i = 1 To $var[0]

$total = $total + $var[$i]

Next

FileClose($outputfile1)

FileWriteLine($Debuglog, @CRLF & "")

FileWriteLine ($Debuglog, "total testcases: " & $total)

EndFunc

*****************************************

Here's the textfile:

***********************************

5

3

8

15

6

7

9

7

7

8

4

4

7

9

7

5

4

8

12

5

5

5

5

6

5

5

5

5

11

5

5

5

5

5

5

9

6

7

3

3

7

7

3

14

2

5

18

3

6

1

5

****************************************

-max

Share this post


Link to post
Share on other sites

Not sure why, but it's only reading the first line of the textfile and using that as a 'total', but if I run my main script again, it'll sum up all the lines and include the first line again in the sum too.

Now, I'm reading in my values from an array like this, so I'm not using @CRLF. If I use @CRLF in the _ArrayToString, I don't seem to get ANYTHING dumped out into the .txt file:

$outputfilename1 = _ArrayToString($aAttrValue, @CR, 3, 3)

The function and the textfile are both below:

Here's the function:

**********************

Func Sum_TestCases1 ()

$outputfile1 = FileOpen(@ScriptDir & "\Scorecard_Testcases1.csv", 0)

sleep(1000)

FileClose($outputfile1)

$outputfile1 = FileOpen(@ScriptDir & "\Scorecard_Testcases1.csv", 0)

sleep(1000)

$total = 0

$var = StringSplit(FileRead("C:\A_working\sprint3\Scorecard_Testcases1.txt", FileGetSize("C:\A_working\sprint3\Scorecard_Testcases.txt")), @CR)

;FileWriteLine($Debuglog, "$var = " & $var)

For $i = 1 To $var[0]

$total = $total + $var[$i]

Next

FileClose($outputfile1)

FileWriteLine($Debuglog, @CRLF & "")

FileWriteLine ($Debuglog, "total testcases: " & $total)

EndFunc

*****************************************

Here's the textfile:

***********************************

5

3

8

15

6

7

9

7

7

8

4

4

7

9

7

5

4

8

12

5

5

5

5

6

5

5

5

5

11

5

5

5

5

5

5

9

6

7

3

3

7

7

3

14

2

5

18

3

6

1

5

****************************************

-max

you could just do something like this:

$file = fileopen("numbers.txt",0)
$total = 0
While 1
$line = filereadline($file)
if @error then exitloop
$total = $total + Number($line)
Wend
MsgBox(0,"Total",$total)

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

Well, it's adding up the first two rows... but that's it.

??

max

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

TESTED... and it works with your number text

#include <file.au3>

Dim $The_File = @ScriptDir & "\Numbers.txt"
Dim $aRecords, $total

If Not _FileReadToArray($The_File,$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
For $x = 1 to $aRecords[0]
    $total = $total + Number($aRecords[$x])
Next
MsgBox(0,"Total",$total)

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

Well, it's adding up the first two rows... but that's it.

??

max

is there white space around the numbers? random spaces etc... i'm not sure why the first two suggestions wouldn't work

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

As MHz pointed out to me in another thread, my stringsplit line should have the flag on it for this use. I've added that to the code below:

$total = 0
$var = StringSplit(FileRead("num.txt", FileGetSize("num.txt")), @CRLF, 1)
For $i = 1 To $var[0]
    $total = $total + $var[$i]
Next
MsgBox(0, 'total', $total)
Does that code work as a stand alone test script - for now, don't incorporate it into your code, just run it as is - with your filenames...

...and be careful with those file names:

$var = StringSplit(FileRead("C:\A_working\sprint3\Scorecard_Testcases1.txt", FileGetSize("C:\A_working\sprint3\Scorecard_Testcases.txt")), @CR)

You are getting the file size of one file - but reading another one.

Scorecard_Testcases1.txt

Scorecard_Testcases.txt


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

As MHz pointed out to me in another thread, my stringsplit line should have the flag on it for this use. I've added that to the code below:

$total = 0
$var = StringSplit(FileRead("num.txt", FileGetSize("num.txt")), @CRLF, 1)
For $i = 1 To $var[0]
    $total = $total + $var[$i]
Next
MsgBox(0, 'total', $total)
Does that code work as a stand alone test script - for now, don't incorporate it into your code, just run it as is - with your filenames...

...and be careful with those file names:

$var = StringSplit(FileRead("C:\A_working\sprint3\Scorecard_Testcases1.txt", FileGetSize("C:\A_working\sprint3\Scorecard_Testcases.txt")), @CR)

You are getting the file size of one file - but reading another one.

Scorecard_Testcases1.txt

Scorecard_Testcases.txt

Hi,

I caught that one earlier, with the filenames, and made the adjustment. Thanks. I'm running this as a function within my script right now and it's still only pulling in the first line of the text file. I just included the flag, but I'm still only able to sum the first line of the textfile. I keep thinking it might be the call I'm making in the function preceeding this one:

$outputfilename1 = _ArrayToString($aAttrValue, @CR, 3, 3)

but I'm just not sure. I'm taking an xml file, writing out the array values above to a text file and then trying to sum up the lines in the textfile, but it's just not happening.

_I've tried a bunch of things and I keep getting the same results. I'll try to break this out into its own script, but not sure how I'll do that yet since this function calls other sub-functions. I've been wrapped around this for many hours now and haven't been successful. I'll do some more things and let you know.

Thanks for all the help!!

-max

Share this post


Link to post
Share on other sites

Hi,

I caught that one earlier, with the filenames, and made the adjustment. Thanks. I'm running this as a function within my script right now and it's still only pulling in the first line of the text file. I just included the flag, but I'm still only able to sum the first line of the textfile. I keep thinking it might be the call I'm making in the function preceeding this one:

$outputfilename1 = _ArrayToString($aAttrValue, @CR, 3, 3)

but I'm just not sure. I'm taking an xml file, writing out the array values above to a text file and then trying to sum up the lines in the textfile, but it's just not happening.

_I've tried a bunch of things and I keep getting the same results. I'll try to break this out into its own script, but not sure how I'll do that yet since this function calls other sub-functions. I've been wrapped around this for many hours now and haven't been successful. I'll do some more things and let you know.

Thanks for all the help!!

-max

Okay, I broke out the function that sums up the textfile into a separate script, and sho' nuff, it works as it should. It adds up all the numbers correctly.

Now, why the heck does it work OUTSIDE the script? If I have a function that writes to a file, then closes that file, then sleeps for a second, then the next function opens that file with a DIFFERENT filehandle and opens it for reading - it just won't add up the numbers correctly.

weird.

anyone have any explanations for this behavior?

-max

Share this post


Link to post
Share on other sites

Are you still working with the code that you showed in post #6?

In that code you open a file in the read mode and then close it without reading from it.

Then you open it again in the read mode???

Download the lastest beta (at least 108) and test this untested code for your function:

Func Sum_TestCases1()
    
    $outputfile1 = FileOpen(@ScriptDir & "\Scorecard_Testcases1.csv", 0)
    If $outputfile1 = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf
    
    $total = 0
    $var = StringSplit(FileRead($outputfile1)), @CR)
    MsgBox(0, "$var[0] how many elements in the array", $var[0])
    
    For $i = 1 To $var[0]
        $total += $var[$i]
        MsgBox(0, "Debug", "$var[" & $i & "] = " $var[$i] & @CR & "total = " & $total)
    Next
    FileClose($outputfile1)
    FileWriteLine($Debuglog, @CRLF & "")
    
    FileWriteLine($Debuglog, "total testcases: " & $total)
    
EndFunc  ;==>Sum_TestCases1

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0