KANlFUSA

Autoit total numbers inside notepad file (solved)

8 posts in this topic

#1 ·  Posted (edited)

any idea to make an autoit script auto total the numbers inside notepad file and show the result in msgbox?

like' 133+34+55+41+639.3 = msgbox the total amount.

 

 

below is notepad user.txt

-------------------------------------------------------

[user]

133

34

55

41

639.3

 

Input=0

--------------------------------------------------------

Edited by KANlFUSA

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

it is not the best way but should work.

#include <Array.au3>
Local $sData=fileread("user.txt")
$sData=StringMid($sData,1,StringInStr($sData,"Input")) ;just for avoid last number (0) no needed if is 0
Local $aData=StringRegExp($sData,"[-+]?[0-9]*\.?[0-9]+",3)

;~ _ArrayDisplay($aData) array result of numbers

Local $sResult=""
For $i= 0 to UBound($aData)-1
    $sResult&= (($i <> 0) ? "+" : "")  & $aData[$i]

Next

MsgBox(0,"",$sResult & " = " & Execute($sResult))

Saludos

Edited by Danyfirex
1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

here is another way without using regex...you decide which is better for you

$Handle = FileOpen("user.txt",0)
$Read = FileRead($Handle)
$Counter = 0
$Sum = 0
$Exit = True
$Readline = ""
while $Exit 
$Counter += 1
$ReadLine = FileReadLine("user.txt",$Counter)
if @error <> 0 Then
    $Exit = False
    EndIf
$Sum += $ReadLine
WEnd

MsgBox("","",$Sum)

 

Edited by CrypticKiwi
Edit for a better way to detect end of File
1 person likes this

Share this post


Link to post
Share on other sites

Thank you Danyfirex

&

Thank you CrypticKiwi

:P

 

Share this post


Link to post
Share on other sites

probably edge cases aplenty, but shorter:

#include <Array.au3>
Local $sData=FileRead("user.txt")
Local $aData = StringRegExp($sData , "\n(\d+\.*\d*)" , 3)
msgbox(0, '' , execute(_ArrayToString($aData , "+")))

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

This is much better in that it adds the first line and allows for various line terminations, but we might as well allow signed values:

MsgBox(0, 'Results', Execute(StringRegExpReplace($sData, "([+-]?\d+\.?\d*)\R*", "\1+") & '0'))

 


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

@kanifusa if you want read the numbers directly from the running notepad rather than from a file you can use this snippet

if WinExists("[CLASS:Notepad]", "") then $sData = WinGetText(WinGetHandle("[CLASS:Notepad]", ""))

MsgBox(0, 'Results', Execute(StringRegExpReplace($sData, "([+-]?\d+\.?\d*)\R*", "\1+") & '0')) ; <- this by jchd

 


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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