Jump to content

Editing one cell based on another cell within a 2D array


Recommended Posts

I found this script for 2D arrays in another help topic:

;# ====================================================================================
;# 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?
 

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
Share on other sites

  • Moderators

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

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

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 by AlwaysLearning
Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
Share on other sites

  • Moderators

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

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

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.

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...