AlwaysLearning Posted March 30, 2015 Posted March 30, 2015 I found this script for 2D arrays in another help topic: expandcollapse popup;# ==================================================================================== ;# Sample 1 ;# First make 2d array, make sure u know how many entries u need, hire I made 6 rows #include <Array.au3> Global $aArray1[6] $aArray1[1] = "Holger" $aArray1[2] = "Jon" $aArray1[3] = "Larry" $aArray1[4] = "Jeremy" $aArray1[5] = "Valik" _ArrayDisplay($aArray1, '$aArray1') ; show it Global $aArray2[6] $aArray2[1] = "$2" $aArray2[2] = "$1" $aArray2[3] = "$0" $aArray2[4] = "$2" $aArray2[5] = "$1" _ArrayDisplay($aArray2, '$aArray2') ; show it ;# Creating 2D array Global $2D_arr[6][2] ; 6 rows & 3 columns _ArrayDisplay($2D_arr, '$2D_arr') ; show it _fill_2d_array($aArray1, 0) _ArrayDisplay($2D_arr, '$2D_arr') ; show it _fill_2d_array($aArray2, 1) _ArrayDisplay($2D_arr, '$2D_arr') ; show it ;# Loop through given array & pur its contents into 2d Array func _fill_2d_array($_Input_Array, $Column) For $i = 1 To UBound($_Input_Array) -1 $2D_arr[$i][$Column] = $_Input_Array[$i] Next EndFunc The $2D_arr looks similar to what I want to make, but how would I set and edit data for column 2 based on the cell in column 1. For example, Everyone received $1 except Valik. How much money did Holger have? Holger had $2. $2 + $1 = $3 Holger now has $3 (repeat until all people are updated except Valik) Any ideas how to do something like that?
JohnOne Posted March 30, 2015 Posted March 30, 2015 You'd have to explain a bit better, or just leave valik out $aArray2[5] = "$0" AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
water Posted March 30, 2015 Posted March 30, 2015 Something like this? #include <Array.au3> Global $aArray[5][2] $aArray[0][0] = "Holger" $aArray[1][0] = "Jon" $aArray[2][0] = "Larry" $aArray[3][0] = "Jeremy" $aArray[4][0] = "Valik" $aArray[0][1] = 2 $aArray[1][1] = 1 $aArray[2][1] = 0 $aArray[3][1] = 2 $aArray[4][1] = 1 _ArrayDisplay($aArray, "Before adding money") For $i = 0 To UBound($aArray, 1) - 1 If $aArray[$i][0] <> "Valik" Then $aArray[$i][1] = $aArray[$i][1] + 1 Endif Next _ArrayDisplay($aArray, "After adding money") My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
Moderators Melba23 Posted March 30, 2015 Moderators Posted March 30, 2015 AlwaysLearning,Welcome to the AutoIt forums. I would do something like this:#include <Array.au3> ; A better way to declare a 2D array Global $aArray[5][2] = [ _ ["Holger", "$2"], _ ["Jon", "$1"], _ ["Larry", "$0"], _ ["Jeremy", "$2"], _ ["Valik", "$1"]] _ArrayDisplay($aArray, "Original") For $i = 0 To UBound($aArray) - 1 ; If anyone by Valik If $aArray[$i][0] <> "Valik" Then ; Get current total by removing "$" $iTotal = Number(StringReplace($aArray[$i][1], "$", "")) ; Replace with total + 1 $aArray[$i][1] = "$" & $iTotal + 1 EndIf Next _ArrayDisplay($aArray, "Added", Default, 8)Please ask if you have any questions. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
AlwaysLearning Posted March 31, 2015 Author Posted March 31, 2015 (edited) Melba23, I figured it out after looking at your script. The problem was that I didn't understand how to reference/modify data in an array. Now I can see that put simply, when working with a 2D array, the first number in brackets after an array variable ($aArray) is rows, the second number is columns. Everything is treated as an independent variable under the simple tag $aArray. I can edit a cell by just putting $aArray[insert row number starting from 0][insert column number starting from 0] = ___________ In my case, the second column is a straight number so it's as easy as: For $i = 0 to Ubound($aArray) - 1 ; From 0 (first time running this portion of script) to whatever the size of this array is (minus 1), do the following: If $array[$i][0] <> "Valik" Then $array[$i][1] += 1 ; If the data in row [$i] (goes down one by one each time) for column [0] (first column) is NOT "Valik" then add 1 to column [1] (second column) Next ; As stated in the "For" statement, stop looping this section once $i (number of times run) = the size of this 2D array - 1 Please pay special attention to my wording in the comments. That is how I'm understanding each line of code right now. Am I understanding correctly? Do you have any corrections that might help me better describe the logic in plain English? Finally, one question: Why don't I need a line saying $i += 1 inside that "For...Next" statement? Does AutoIt understand by default what I am trying to do with $i in "For...Next" statements? Is that why it is different from a "Do... Until" statement? Thank you for your advice and taking the time to reply. Edited March 31, 2015 by AlwaysLearning
water Posted March 31, 2015 Posted March 31, 2015 Finally, one question: Why don't I need a line saying $i += 1 inside that "For...Next" statement? Does AutoIt understand by default what I am trying to do with $i in "For...Next" statements? Is that why it is different from a "Do... Until" statement? The counter in For ... Next ($i in your case) is incremented automatically every time the loop is being executed. Regarding arrays: There is a very good tutorial in the wiki. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
Moderators Melba23 Posted March 31, 2015 Moderators Posted March 31, 2015 AlwaysLearning,You have the logic correct. As you are obviously new to coding (we all were at some time ) I suggest reading the Help file (at least the first few sections - Using AutoIt, Tutorials and the first couple of References) as this will help you enormously. You should also look at this excellent tutorial - you will find other tutorials in the Wiki (the link is at the top of the page) as water has already suggested. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
AlwaysLearning Posted March 31, 2015 Author Posted March 31, 2015 The counter in For ... Next ($i in your case) is incremented automatically every time the loop is being executed. Regarding arrays: There is a very good tutorial in the wiki. I actually read that before coming here and bothering you good folks. But being new to coding, some of the wording confused and overwhelmed me. I ended up with more questions than answers after reading it, and I probably should have tested a bit more on my own (and will in the future). AlwaysLearning, You have the logic correct. As you are obviously new to coding (we all were at some time ) I suggest reading the Help file (at least the first few sections - Using AutoIt, Tutorials and the first couple of References) as this will help you enormously. You should also look at this excellent tutorial - you will find other tutorials in the Wiki (the link is at the top of the page) as water has already suggested. M23 Thanks. I actually already read a few of those, but reading and FULLY understanding are two very different things. I'll have to go back and reread them more carefully. The learning to script tutorial is especially nice! I hadn't seen that one before and I'm reading it now. Thanks again.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now