Jump to content

Recommended Posts

Posted

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?
 

Posted

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:

  Reveal hidden contents

 

  • Moderators
Posted

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:

  Reveal hidden contents

 

Posted (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 by AlwaysLearning
Posted
  On 3/31/2015 at 5:46 AM, AlwaysLearning said:
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:

  Reveal hidden contents

 

  • Moderators
Posted

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:

  Reveal hidden contents

 

Posted

  On 3/31/2015 at 6:11 AM, water said:

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

 
  On 3/31/2015 at 6:58 AM, Melba23 said:

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.

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
  • Recently Browsing   0 members

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