Results 1 to 10 of 10

Thread: Populating 2d arrays [resolved]

  1. #1

    Thread Starter
    Addicted Member mr_metal_hed's Avatar
    Join Date
    Aug 2002
    Location
    NC
    Posts
    149

    Populating 2d arrays [resolved]

    I know that I can populate a 1d array like this:
    VB Code:
    1. Dim Ax1()
    2. Cat = Array("q", "w", "e", "r", "t", "y", "u", "i", "o")
    Is there a similar way to populate an array of 2d or more? There must be a better way than this:
    VB Code:
    1. Dim Ax2(2, 2)
    2. Ax2(0, 0) = "q": Ax2(0, 1) = "w": Ax2(0, 2) = "e"
    3. Ax2(1, 0) = "r": Ax2(1, 1) = "t": Ax2(1, 2) = "y"
    4. Ax2(2, 0) = "u": Ax2(2, 1) = "i": Ax2(2, 2) = "o"
    Last edited by mr_metal_hed; Feb 4th, 2004 at 11:27 AM.

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    Because each time you are assigning a value to the 2d array, there is no way to loop through and apply values. This is the sort of thing that is used outside of loops.



    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3

    Thread Starter
    Addicted Member mr_metal_hed's Avatar
    Join Date
    Aug 2002
    Location
    NC
    Posts
    149
    I don't understand what you said, but I'll take it as a 'no'.

  4. #4

    Thread Starter
    Addicted Member mr_metal_hed's Avatar
    Join Date
    Aug 2002
    Location
    NC
    Posts
    149
    It seems to me that something like this should work:
    VB Code:
    1. Ax2 = Array(("q", "w", "e"), ("r", "t, "y"), ("u", "i", "o"))
    Since I have no other replies, I assume that there is not way to populate a 2d array by listing arguments. Is there another way to store the same data? An array of arrays maybe? I have not dealt with arrays within arrays and I'm not sure how to go about it, or their similarity with multidimensional arrays.

    Input, ideas, or examples?

  5. #5
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Not sure this is exactly what you are looking for, but you can loop through an multi-dementional array and add values.

    VB Code:
    1. Dim MyArray(10, 10) As String
    2.  
    3. For i = 0 To 10
    4.  For j = 0 To 10
    5.    MyArray(i, j) = "Num" & i + j
    6.  Next
    7. Next
    8. MsgBox MyArray(7, 9)
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  6. #6

    Thread Starter
    Addicted Member mr_metal_hed's Avatar
    Join Date
    Aug 2002
    Location
    NC
    Posts
    149
    That won't work because my array values aren't sequential. I can't just use an increment to assign them. The array values are constant. I use them for comparison to evaluate input.

    Thanks anyway!

  7. #7
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    You could just use a 1D array, and have functions to access it as if it was a 2D array.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  8. #8

    Thread Starter
    Addicted Member mr_metal_hed's Avatar
    Join Date
    Aug 2002
    Location
    NC
    Posts
    149
    You are right. I could use an increment and the Mid() function to loop through each position in the data within each array item to make it act like a 2d array. But if I wanted to do that, I wouldn't bother using arrays at all. I could just use one long string, taking data from the necessary position as needed.

    Right now, the 2d array is working out fine. I just don't like that there isn't an easy way to populate it.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    I don't think he meant to put a string in each 1d array slot and Mid$ it. A 2d array is probably layed out sequentially in memory (memory is sequential, anyways). Therefore, if you have an array with dimensions of 3 and 4, you could arrange it in a 1d array like 111122223333. To find array(x,y) you would do something like

    x * <size of dimension 2> + y. This would be a one day array.

  10. #10

    Thread Starter
    Addicted Member mr_metal_hed's Avatar
    Join Date
    Aug 2002
    Location
    NC
    Posts
    149
    Okay, I get the concept now. I decided to use a 8x3 array to store my data. The 2d array that I have discussed was just to make asking questions simpler. Since assigning 24 values in a multidimensional array isn't a major task, I will stick with what I have. If I had more values, this 1d aka fake 2d array might be useful. Also, I already have it coded... Thanks for the idea.

    I guess I will mark this thread as resolved since my initial question has been answered.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width