Sign in to follow this  
Followers 0
gr1fter

Adding Array Values

4 posts in this topic

I'm having difficulties with this script I am creating.  Arrays seem to be my hardest learning curve but I'm trying to get better...

what i am trying to do is this.  I have a job that is creating a log file, the contents example of the log are like this:

2014-10-30_1132 AM,test,H142640E4,Computer  ,11.231.42.101,Name,11:31:17:109,11:31:25:389,11:31:37:775,11:31:39:164,22,9
2014-10-30_1140 AM,test,H142640E4,Computer  ,11.231.42.101,Name,11:39:56:516,11:40:03:652,11:40:07:583,11:40:08:971,12,800  
2014-10-30_1140 AM,test,H142640E4,Computer  ,11.231.42.101,Name,11:39:56:516,11:40:03:652,11:40:07:583,11:40:08:971,12,12
2014-10-30_1140 AM,test,H142640E4,Computer  ,11.231.42.101,Name,11:39:56:516,11:40:03:652,11:40:07:583,11:40:08:971,12,4552

the only number i need in these values are the last numbers after the last comma.  I wrote this script and so far it is getting me exactly what i need, however I don't know where to go from here, because I want to add these numbers together and divide them by the number of entries (get the average)

This is my current code:

#include <File.au3>
#include <Array.au3>

Global $log= "C:\times.log"
Global $aArray = 0
Global $aTimes = 0


getaverage()



Func getaverage()

If Not _FileReadToArray($log, $aArray, 1) Then
    MsgBox(48, "", "There was an error reading the file. @error: " & @error)
Else
    MsgBox(64, "GetTime Info","Number of entries that have been logged: " & $aArray[0])
    For $i = 1 To UBound($aArray) - 1
        $aTimes = StringSplit($aArray[$i],",",1)
        MsgBox(64, "GetTime Info","Time in Seconds: " & $aTimes[12])
    Next

EndIf

EndFunc

Am I on the right track or doing this completely wrong?  Thanks!

Share this post


Link to post
Share on other sites



Yes, you are getting there. Create an array to fill for your adding up the average. First, you'll just need to set a variable to the last value in the split array using it like so $i = $aTimes[0].

Then you'll need to set that value you get the in the element of the array _ArrayAdd($adding_Array, $aTimes[$i]).

Once you had added all the values into the array, use Ubound($adding_Array) - 1 to get the number of elements in the array.

Then, use a for loop to add them up, and divide by the Ubound($adding_Array) - 1 value.

Any questions? :)

1 person likes this

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

gr1fter,

Given that the numbers you are looking for are easily extracted with a RegEx, I would do something like this:

; Read the entire file in one go
$sText = "2014-10-30_1132 AM,test,H142640E4,Computer  ,11.231.42.101,Name,11:31:17:109,11:31:25:389,11:31:37:775,11:31:39:164,22,9" & @CRLF & _
    "2014-10-30_1140 AM,test,H142640E4,Computer  ,11.231.42.101,Name,11:39:56:516,11:40:03:652,11:40:07:583,11:40:08:971,12,800" & @CRLF & _
    "2014-10-30_1140 AM,test,H142640E4,Computer  ,11.231.42.101,Name,11:39:56:516,11:40:03:652,11:40:07:583,11:40:08:971,12,12" & @CRLF & _
    "2014-10-30_1140 AM,test,H142640E4,Computer  ,11.231.42.101,Name,11:39:56:516,11:40:03:652,11:40:07:583,11:40:08:971,12,4552"

; Extract the final number - look for a comma, a string of digits and either an EOL or EOF
$aRet = StringRegExp($sText, ",(\d+)(?:\v|$)", 3)

; Add the extracted numbers
$iTotal = 0
For $i = 0 To UBound($aRet) - 1
    $iTotal += $aRet[$i]
Next
; And divide by the count
$nAverage = $iTotal / UBound($aRet)

; And the result looks correct to me
ConsoleWrite($nAverage & @CRLF)
I think that using a RegEx will be faster then splitting each line, particularly if the file is very large, but your approach should also work if you prefer it. :)

M23

2 people like this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

gr1fter,

Given that the numbers you are looking for are easily extracted with a RegEx, I would do something like this:

; Read the entire file in one go
$sText = "2014-10-30_1132 AM,test,H142640E4,Computer  ,11.231.42.101,Name,11:31:17:109,11:31:25:389,11:31:37:775,11:31:39:164,22,9" & @CRLF & _
    "2014-10-30_1140 AM,test,H142640E4,Computer  ,11.231.42.101,Name,11:39:56:516,11:40:03:652,11:40:07:583,11:40:08:971,12,800" & @CRLF & _
    "2014-10-30_1140 AM,test,H142640E4,Computer  ,11.231.42.101,Name,11:39:56:516,11:40:03:652,11:40:07:583,11:40:08:971,12,12" & @CRLF & _
    "2014-10-30_1140 AM,test,H142640E4,Computer  ,11.231.42.101,Name,11:39:56:516,11:40:03:652,11:40:07:583,11:40:08:971,12,4552"

; Extract the final number - look for a comma, a string of digits and either an EOL or EOF
$aRet = StringRegExp($sText, ",(\d+)(?:\v|$)", 3)

; Add the extracted numbers
$iTotal = 0
For $i = 0 To UBound($aRet) - 1
    $iTotal += $aRet[$i]
Next
; And divide by the count
$nAverage = $iTotal / UBound($aRet)

; And the result looks correct to me
ConsoleWrite($nAverage & @CRLF)
I think that using a RegEx will be faster then splitting each line, particularly if the file is very large, but your approach should also work if you prefer it. :)

M23

 

 

Thank you very much.  I love that there are multiple ways to reach an end goal.  You were correct on the StringRegExp, it is doing it faster and it appears that it is averaging out correct.  Thanks again!

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