Jump to content

Array calculations help


Recommended Posts

Hi,

I'm fairly new to autoit and I'm trying to find my way through a little problem...

I have to analyze some csv files (log files). They come splitted in many files, with the first row containing the name of the columns. So the first task is combining them, keeping just the first row of the first file, and removing the first row of each subsequent file, which I easily managed to do (with the help of some example here and there). At this point I usually import the single file into excel and perform some calculations. This lends to quite large excel files, and it's overkill, because once the calculations are done I no longer need the source raw data. So it would be best to perform the basic calculations in autoit and import just the results into Excel to complete the remaining tasks. I guess the best way would be loading the data into a multidimensional array, and then manipulate them like if it was a spreadsheet table. And here's my problem, I'm not too acquainted with arrays...

The calculations I need to perform are these:

1) find the total number of cells (the max size of the array, really). This should be easy

2) find how many times the value in Column(A) is greater than 1000

3) for each row where Column(A) is greater than 1000, I need to calculate the difference between the same row on another column and its preceding row (for instance in Excel it is : IF(A143>1000; B144-B143; "") )

The point is... from were do I start in performing math operations on multidimensional arrays?

Any help is greatly appreciated.

Luc

Link to comment
Share on other sites

  • Moderators

settecplus,

2D arrays are in fact just like an Excel spreadsheet - you can think of the first dimension as the row and the second the column. :)

I suggest loading one of these CSV files into an array (if you have the Beta version you can use _FileReadToArray - if not there are plenty of CSV2Array type examples out there) and then using _ArrayDisplay to check how the data is presented. That should allow you to determine how you need to structure the formulae for your calculations. ;)

If you get stuck, I suggest posting a small sample of one of these log files and explaining which elements need to be used in the various calculations you want to make. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

M23,

thank you for your help. I've already done what you suggested, and I have used _ArrayDisplay to take a look at my data. I'm really stuck at math calculations with arrays :)

I have attached a (very) stripped down csv file.

I need to calculate:

- the number of rows of column A (in this case 14)

- how many times column I is greater than 1000 (in this case 9)

- this is more complicate: whenever row x of column I is > 1000, I need to calculate the sum of all of the results of row x (column B ) = [row x (column B )] - [row x-1 (column B )]. To be more clear, in Excel I perform this task by creating a new temporary column (Z), which is populated by a IF((I5>1000;B6-B5;"")), and I then do a SUM(Z1:Z1000000).

 

 

Luc

test_array.txt

Edited by Melba23
Removed inadvertant emoticons
Link to comment
Share on other sites

  • Moderators

settecplus,

This works for me - do the results match what you want? :huh:

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

$aArray = _FileReadToArray("test_array.txt", $FRTA_NOCOUNT, ";")

_ArrayDisplay($aArray, "", Default, 8, Default, "A|B|C|D|E|F|G|H|I|J|K")

; Number of rows
MsgBox($MB_SYSTEMMODAL, "Rows", UBound($aArray))

; Rows where Col I > 1000
$iCount = 0
; Loop through the array 
For $i = 0 To UBound($aArray) - 1 ; Because the array starts at 0 we need to reduce the count by 1
    ; Note we use numeric, not alpha, indices
    If $aArray[$i][8] > 1000 Then $iCount += 1
Next
MsgBox($MB_SYSTEMMODAL, "Rows where Col I > 1000", $iCount)

; Complicated bit
$nTotal = 0
For $i = 0 To UBound($aArray) - 2 ; Because the calculation accesses a line greater the current one we need to reduce the count by 2
    ; Is Col I > 1000
    If $aArray[$i][8] > 1000 Then
        ; Calculate value and add to total - replace commas with decimal points
        $nTotal += StringReplace($aArray[$i + 1][1], ",", ".") - StringReplace($aArray[$i][1], ",", ".")
    EndIf
Next
MsgBox($MB_SYSTEMMODAL, "Complicated total", StringReplace($nTotal, ".", ",")) ; replace decimal point with comma
M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

  • Moderators

settecplus,

Glad I could help. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

M23,

I guess I may need some more help...

Your script works perfectly with new Autoit beta, but on latest stable version I get the error:

"warning: $FRTA_NOCOUNT: possibly used before declaration"

At the same time, the other part of my script (the one which deals with csv merging and manipulating) works on latest stable but not on newer beta. In this case it doesn't even show any error, so I'm having troubles debugging the error...

Long story short, one part of the script works only on newer alpha, the other part works just with the older stable. Quite uncomfortable :)

Luc

Link to comment
Share on other sites

  • Moderators

settecplus,

You can replace $FRTA_NOCOUNT with the integer 0 - and give guinness indigestion, but that is not your problem. ;)

If you want to post the script you are using that fails with the Beta (or send it to me via PM) I will take a look at it and see if I can spot the problem. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
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
 Share

  • Recently Browsing   0 members

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