Jump to content

Recommended Posts

Posted

I have a text file which look like this. I'd like to add these all up to get the sum, but can't seem to figure it out :ermm: Any tips on how I can do this (sure it's really easy but I can't think of it).

Posted

You can read the file line by line (look at the helpfile for FileReadLine), and then add each number to the sum (starting to 0)

$sum = 0

While 1

 $line = FileReadLine(......)

 $sum += Number($line)

WEnd

ConsoleWrite($sum)

You can also use _FileReadToArray and make a For...Next loop

Posted (edited)

You will need the help file and...

_FileReadToArray()
Int()/Number()
+
=
Loop e.g. For
Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Here one trick(box):
 

Global $aZahlen = StringSplit(FileRead(@ScriptDir & "\Zahlen.txt"), @CRLF, 2), $i, $iSumme
For $i = 0 To UBound($aZahlen) - 1
    $iSumme += $aZahlen[$i]
Next
MsgBox(0, "Summe", $iSumme)

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

  • Moderators
Posted (edited)

Keep in mind, the below example won't help with "big numbers"

Func _sumFile($sFile)
    
    Local $sFRead = $sFile
    If FileExists($sFRead) Then
        $sFRead = FileRead($sFile)
    EndIf
    
    Local $aNums = StringRegExp($sFRead, "\d+(?:\.d+)?", 3)
    If @error Then Return SetError(1, 0, 0)
    
    Local $iSum = 0
    For $i = 0 To UBound($aNums) - 1
        $iSum += Number($aNums[$i])
    Next
    
    Return $iSum
EndFunc
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Posted

The issue with those who have suggested splitting at @CRLF, is what if the file has line-feed as the EOL char?

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

  On 1/15/2015 at 10:30 PM, Valuater said:

Simple Valuater... 8)

 

FileRead()

 

Stringreplace( $String, @CRLF, "+")

 

Number($String)

 

Tada!!!

 

8)

 

 

Brilliant idea but one line is not correct!

 

Instead of Number($String) you have to use Execute($String) ;)

 

Global $sAddition = Stringreplace(FileRead(@ScriptDir & "\Zahlen.txt"), @CRLF, "+")
MsgBox(0, "Summe", Execute($sAddition))
Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...