Jump to content

Recommended Posts

Posted (edited)

These things have always made my head hurt for some reason, and I can't seem to figure this out.

I want an array to hold a list of names, and each name has 3 attributes that go with it, Address, Phone#, and Zip Code.

So the first element might look like: ["John"] ["987 Foo St"] [5555555555] [90210]

I then need to be able to search through the first element, the Name, until a match was found, then extract the 3 corresponding attributes

Psuedocode:

Local $MyArray[x][x][x][x] = ?
Local $SearchingFor = "Fred"
Local $Address
Local $Phone
Local $Zip

For $i = 0 To 20 ;I know how many names are in the list
If $SearchingFor = $MyArray[$i] Then
$Address = $MyArray[$i][AddressIsHere][][]
$Phone = $MyArray[$i][][PhoneIsHere][]
$Zip = $MyArray[$i][][][ZipIsHere]
ExitLoop
Next

I'm probably way off target, or completely misunderstanding how MD arrays work. Am I even close, and if so, does anyone have a short example? The wiki just confused me, but that's probably because it used all numbers instead of different categories. The key point is I need a way to have four variables that are tied to each other with a way to search through the first one with a for/to loop in order to extract the other three. I have well over 500 sets of data to sort through, and I'd rather not make a Select-Case/If-Then loop that big with over 500 separate variables.

Edited by JSands
Posted

Here is an example of a 2D multi-dimensional array.

#include <Array.au3>

Local $MyArray[20][4] = _
        [["Name", "Address", "Phone#", "Zip Code"], _
        ["John", "987 Foo St", "5555555555", 90210], _
        ["Fred", "223 Fi St", "5555555556", 90211]]
_ArrayDisplay($MyArray)

Local $SearchingFor = "Fred"
Local $Address
Local $Phone
Local $Zip
Local $iIndex = _ArraySearch($MyArray, $SearchingFor)

MsgBox(0, "Found " & $SearchingFor & " @ index " & $iIndex, $MyArray[$iIndex][0] & " " & $MyArray[$iIndex][1] & " " & $MyArray[$iIndex][2] & " " & $MyArray[$iIndex][3])
Posted

You must read the wiki article about arrays.

Dim $MyArray[5][4] = _
[["John","897 Foo St","5555555555","90210"], _
["Jim","53 FooBar St","3333333333","98642"], _
["Paul","1 Foo Ln","2222222222","67361"], _
["Fred","356 Bar St","4444444444","23562"], _
["Andy","16 Bar Ave","1111111111","48036"]]

$SearchingFor = "Fred"

For $i = 0 To UBound ($MyArray) -1 ; Array is zero based
    If $MyArray[$i][0] = $SearchingFor Then ExitLoop ; No need to carry on searching
Next

Dim $Address, $Phone, $Zip

If $i < UBound ($MyArray) Then ; The loop was terminated on finding a match
    $Address = $MyArray[$i][1]
    $Phone = $MyArray[$i][2]
    $Zip = $MyArray[$i][3]
    ConsoleWrite($SearchingFor & "|" & $Address & "|" & $Phone & "|" & $Zip)
EndIf
Posted (edited)

Here is an example of a 2D multi-dimensional array.

Perfect example, now I get it. I swear, every example I've seen uses a bunch of integers that makes it difficult to follow. That _ArrayDisplay is a cool function I hadn't seen yet, really made it simple to see what happened and what I need to do. Thanks again!

You must read the wiki article about arrays.

As mentioned in my first post, I read that and the part about MD Arrays confused me. I saw this:

Local $arr[3][3][3][3] = [[[[1, 2, 3],[2, 3, 4],[3, 4, 5]], _
        [[1, 2, 3],[2, 3, 4],[3, 4, 5]],[[1, 2, 3],[2, 3, 4],[3, 4, 5]]], _
        [[[1, 2, 3],[2, 3, 4],[3, 4, 5]],[[1, 2, 3],[2, 3, 4],[3, 4, 5]], _
        [[1, 2, 3],[2, 3, 4],[3, 4, 5]]],[[[1, 2, 3],[2, 3, 4],[3, 4, 5]], _
        [[1, 2, 3],[2, 3, 4],[3, 4, 5]],[[1, 2, 3],[2, 3, 4],[3, 4, 5]]]]

And had no idea which set was being assigned and to where. Malkey's example, and the one you posted as well, are much more easy to follow than the wiki. Thanks again to you both.

Edited by JSands
Posted

Yes that example is a four dimensional array (which is perhaps a little hard to imagine). You only need a two dimensional array. I use _ArrayDisplay a lot to check that the contents of a one or two dimensional array are correct. You will become more familiar with arrays with practice. Happy to assist. :)

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