Jump to content

Need Help Understanding Arrays


Recommended Posts

Hi, again, everyone. I'm glad to report that I've really been working with my AutoIt scripting for the past few months and have been getting a lot better, I'm able to get my programs to do just about everything I want. However, I have been running into problems with arrays. I've made an extensive effort to understand how they work but it's just not clicking for me. For the most part I've been avoiding arrays in my scripts but now I have to use them for the function I need. Let me at least explain what I understand and then maybe someone can correct me and point me in the right direction.

So let's say I'm working with inireadsectionnames. We'll say $sectionget = IniReadSectionNames ( "C:\log.ini", "section1" )

And we'll say log.ini has

[section 1]

a=1

b=2

c=3

So $sectionget should return an array with as described in the help file as $sectionget[n][0] and $sectionget[n][1]

I don't know if I'm not understanding the context of arrays, but what does [n] represent here? Ultimately what I want to do is to count the number of keys and dump the element data into a list or something. I could imagine counting the number of arrays with something like

Switch $sectionget
If $sectionget[thearraythatreturnsthekeys] < [1] Then
$thenumberofkeys = "0"
ElseIf $sectionget[thearraythatreturnsthekeys] < [2] Then
$thenumberofkeys = "1"
...
Endswitch

It's the part where the array returns the keys or how many array slots they take...

I really appreciate any help you guys can give me.

Link to comment
Share on other sites

The first of the two dimensional array subscripts is holding the number of the records in the array, so in your example

$sectionget[0][0] return the highest subscript which is [3][0] for the value name and [3][1] for it's value. No matter how much subscripts your array holds it can always return 1 value at a time, no matter if it's [1][2] or [2][1] it's still same in memory.

Link to comment
Share on other sites

Here's a post I did once to try to help someone understand arreays. There are also some usefule links in later posts.
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

The first of the two dimensional array subscripts is holding the number of the records in the array, so in your example

$sectionget[0][0] return the highest subscript which is [3][0] for the value name and [3][1] for it's value. No matter how much subscripts your array holds it can always return 1 value at a time, no matter if it's [1][2] or [2][1] it's still same in memory.

Thanks very much for your help. I think I'm beginning to understand better.

So the first subscript will return how many records it has accumulated. That is, how many keys it found. So if there were 20 keys it would return $sectionget[20]. So, you're saying that the second subscript returns the DATA for the keys and elements? So does that mean that $sectionget[20][1] refers to the data of the first key and $sectionget[20][2] refers to the data of the element for the first key? So that means that $sectionget[20][3] would refer to the second key found? And $sectionget[20][4] would refer to the element for the second key?

Link to comment
Share on other sites

$sectionget[0][0] would contain the amount (or you could use the Ubound() as not all functions will do this )

$sectionget[1][0] would contain the first key

$sectionget[1][1] would contain the first value

$sectionget[2][0] would contain the second key

$sectionget[2][1] would contain the second value

etc....

Link to comment
Share on other sites

$sectionget[0][0] would contain the amount (or you could use the Ubound() as not all functions will do this )

$sectionget[1][0] would contain the first key

$sectionget[1][1] would contain the first value

$sectionget[2][0] would contain the second key

$sectionget[2][1] would contain the second value

etc....

Ah, I see, this lays it out for me very well. I will put this to the test in just a bit.
Link to comment
Share on other sites

The explanation in the Wiki is quite good, read that if you have more problems with arrays LINK

Edited by AdmiralAlkex
Link to comment
Share on other sites

The explanation in the Wiki is quite good, read that if you have more problems with arrays LINK

Yes, I read that as part of my research and it did answer some questions but it really wasn't clear to me. I have a much better understanding now and have my program doing what I want again. Thanks very much, everyone!

Link to comment
Share on other sites

(Sorry if you're way past this by now, but this may be a different take on describing multi-dimension arrays, or maybe this wil be useful to someone else someday..)

Don't let the number of subscripts (or dimensions) in an array throw you off. You can think of the last subscript as the only one that contains your data, and the rest of them just multipliers, that allow you to group your data elements logically.

The only purpose of arrays is to simplify coding, in both the creation of variable names, and the processing of those variables.

Say there are 2 ball leagues, each with 20 teams and each team has 10 players. That's 2*20*10 or 400 players total.

If you wanted to store, and report on, let's say the RBI statistics for each player, without arrays you have to create 400 unique variables like:

Dim $player001 = .321, $player002 = .256, $player003 = .347, $player004 = .211, $player001 = .278,...; 395 more here

and would need 100's of lines of code to manipulate individually like:

Msgbox(1, "", "Player 1's RBI average is: " & $player001)
Msgbox(1, "", "Player 2's RBI average is: " & $player002)
Msgbox(1, "", "Player 3's RBI average is: " & $player003) 
; (397 more msgbox commands here)

So, here's the same thing with a single-dimension array where the subscript contains data and represents 'player':

Dim $Player[400] = [.321, .256, .347, .211, .278....]; All 400 entries hard-coded in the array declaration
For $i = 1 to 400
    Msgbox(1, "", "Player " & $i & "'s RBI average is: " & $player[$i])
Next

If you had reason to want to modify/report on your data divided by league, you could convert to a two-dimension array:

Dim $Player[2][200] = [[.321, .256, .347, .211, .278....],[200 more entries for second league...]]

It's the exact same data, just divided logically, the first-dimension representing "league" contains no data, the second-dimension "player" is where the data still is, it's just divided into 2 groups and needs to be referenced that way:

For $i = 1 to 2
    Msgbox(1, "", "RBI statistics for players in League " & $i)
    For $j = 1 to 200
        Msgbox(1, "", "League player " & $j & "'s RBI average is: " & $player[$i][$j])
    Next
Next

Take it farther and you get a three-dimension array "league/team/player". There's really nothing 'contained' in the league and team subscripts, they are just groupings/multipliers for the final subscript, player, that "contains" your data.

Dim $Player[2][20][10] = [[[.321, .256, .347, .211, .278....],[19 more teams],[20 more teams]]]
$league = 1
$team = 8
$player = 3
Msgbox(1, "", "Player " & $player & " for Team " & $team & "'s RBI average is: " & $player[$league][$team][$player])

To make life easy, my examples assume a "one-based" array system, Autoit's arrays are zero-based. Meaning, the first extry in any dimension is named 0, not 1. That requires putting "+ 1" or "- 1" in code often to make things work/appear correctly:

Dim $Player[400] = [.321, .256, .347, .211, .278....]; All 400 entries hard-coded in the array declaration
For $i = 0 to 399
    Msgbox(1, "", "Player " & ($i + 1) & "'s RBI average is: " & $player[$i])
Next

Of course, hard-coding a large number of array values into a Dim/Local/Global/Const declaration is a pain in the butt. So using loops to load the data from a file into the array is common (Autoit has the _FileReadTo Array and _FileWriteFromArray functions to make that cake)

(Note: The "just think of your data as residing in the last subscript" idea doesn't apply to all programming languages)

Well, anyway, that's how I mght try to explain things to someone newly chewing on the concept of multi-dimension arrays.

Edited by Spiff59
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...