Results 1 to 19 of 19

Thread: Array of Arrays? and Array["howdy"]?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    8

    Array of Arrays? and Array["howdy"]?

    Im trying to figure out a way that I could make an array of references to arrays (or even just arrays for that matter) for the purpose of creating a hash table, but i cant find a way to do this without creating a new object type that just has an array variable in it. Is there another way ( i dont want to use a two dimensional array, that will waste too much space).

    Also, does vb support arrays like php? I dont know the technical ter for this type of array (i think its reference array), but they work like this:

    Array["arrayidx1"], Array["arrayid2"] .... Array["idxn"]

    thanks in advance.

  2. #2
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    299
    An array of variants, use this as an example.
    Dim YourArray(10) As Variant
    Dim vSubArray As Variant

    ReDim vSubArray(10)
    vSubArray(0) = "Wow"
    YourArray(0) = vSubArray

    MsgBox YourArray(0)(0)

  3. #3
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    299
    please tell me if that works for you. I hope that it helped

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You could use a variant, which can hold an array.

    VB Code:
    1. Dim Master(1) As Variant
    2.     Dim Element1 As Variant
    3.     Dim Element2 As Variant
    4.    
    5.     Element1 = Array("Ed", "Cookie", "Mike")
    6.     Element2 = Array("Admin", "VP", "DQ")
    7.     Master(0) = Element1
    8.     Master(1) = Element2
    9.    
    10.     MsgBox Master(0)(1) 'shows cookie

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    8
    im trying it as we speak

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Damn I type too slow. Although I did test it and it works.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    8
    well, i wasnt able to test the code, but it looks good.. the only thing I'm not sure about based on the code examples is this:

    Assume that all of the data to be inserted into the array element 1 is not known at the time of creation. Would it be possibe to redim the array on the fly using this method?

    Also, can anyone answer if vb supports arrays of the format array["stridx1"], array["stridx2"], array["stridxn"] ?

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Sure you can use a dynamic array instead. Also I did test the code I posted, which is the same as SnakeEyes and it worked just fine.

    VB Code:
    1. Dim Master() As Variant
    2.     Dim Element1 As Variant
    3.     Dim Element2 As Variant
    4.     Dim Element3 as Variant
    5.    
    6.     Element1 = Array("Ed", "Cookie", "Mike")
    7.     Element2 = Array("Admin", "VP", "DQ")
    8.     Element3 = Array(25, 49, 26)
    9.  
    10.     ReDim Master(1)
    11.     Master(0) = Element1
    12.     Master(1) = Element2
    13.    
    14.     MsgBox Master(0)(1) 'shows cookie
    15.  
    16.     'extend array
    17.     ReDim Preserve Master(2)
    18.     Master(2)=Element3
    19.  
    20.     MsgBox Master(2)(0) 'shows 25

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    8
    ok, but the problem with the code you have presented there is that for every redim of the Master variable, a new elementn variable is required. I experimented with code like the following but had no luck.

    j=1
    k=1
    dim Master() as variant
    dim Hash() as variant
    redim master(j)
    redim hash(k)
    master(j) = hash(k)

    redim master(0)(k+1) ' we need to add another hashed value which mapped to position 0

  10. #10
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    I posted something like this a while back and didn't come up with an answer. I wanted an array of arrays so that each array could have a different number of dimensions. But when I tried to redimension the first array it didn't work.

    Arr()()

  11. #11
    Megatron
    Guest
    Isn't this the same as a 2D array?
    Code:
    Dim MyArray(10, 10) As String

  12. #12
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Originally posted by Megatron
    Isn't this the same as a 2D array?
    Code:
    Dim MyArray(10, 10) As String
    It shouldn't be.

    You should be able to have something where each array in the arrays has a different number of dimensions. If you do what you're showing then all the arrays (second dimension) must have the same number of dimensions. But I couldn't get it to work.

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You just have to be tricky with it, but it can be done look:
    VB Code:
    1. Dim Master() As Variant
    2.     Dim Element1 As Variant
    3.     Dim Element2 As Variant
    4.     Dim Element3 As Variant
    5.    
    6.     Element1 = Array("Ed", "Cookie", "Mike")
    7.     Element2 = Array("Admin", "VP", "DQ")
    8.     Element3 = Array(25, 49, 26)
    9.  
    10.     ReDim Master(1)
    11.     Master(0) = Element1
    12.     Master(1) = Element2
    13.    
    14.     MsgBox Master(0)(1) 'shows cookie
    15.  
    16.     'extend array
    17.     ReDim Preserve Master(2)
    18.     Master(2) = Element3
    19.  
    20.     MsgBox Master(2)(0) 'shows 25
    21.    
    22.     ReDim Preserve Element3(3)
    23.     Element3(3) = "Extra"
    24.     Master(2) = Element3
    25.    
    26.     MsgBox Master(2)(3) 'shows extra but still holds the values that were there before
    27.     'and it now has an upperbound of 3 where the other elements still only have 2

    The thing that screws it up the normal way is the syntax: redim Master(2)(3)

    But if you break out one element, expand it and put it back then it works.

  14. #14
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    A word of caution though if you are changing the bounds of an element outside of the sub where the element was created then you'll have to make a new variant and assign the element to it then change it and add it back.

    VB Code:
    1. Dim Master() As Variant
    2.  
    3. Private Sub Command1_Click()
    4.     Dim Element1 As Variant
    5.     Dim Element2 As Variant
    6.     Dim Element3 As Variant
    7.    
    8.     Element1 = Array("Ed", "Cookie", "Mike")
    9.     Element2 = Array("Admin", "VP", "DQ")
    10.     Element3 = Array(25, 49, 26)
    11.  
    12.     ReDim Master(1)
    13.     Master(0) = Element1
    14.     Master(1) = Element2
    15.    
    16.     MsgBox Master(0)(1) 'shows cookie
    17.  
    18.     'extend array
    19.     ReDim Preserve Master(2)
    20.     Master(2) = Element3
    21.  
    22.     MsgBox Master(2)(0) 'shows 25
    23.    
    24. End Sub
    25.  
    26. Private Sub Command2_Click()
    27.     Dim Element3 As Variant
    28.     Element3 = Master(2)
    29.  
    30.     ReDim Preserve Element3(3)
    31.     Element3(3) = "Extra"
    32.     Master(2) = Element3
    33.    
    34.     MsgBox Master(2)(3) 'shows extra
    35.    
    36.     MsgBox Master(2)(1) 'make sure it still retained the previous values
    37.  
    38. End Sub

    Of course this is not very conservative considering you have to use variants for it to work. Although on the plus side since you are using a variant then you can store different data types inside the element arrays. (i.e. the 3rd element of the master array has 3 elements that are intergers and 1 that is a string. What are you needing this for? I mean there doesn't seem like a lot of situations where you'd want an array of different arrays.
    Last edited by Edneeis; Jul 10th, 2002 at 09:45 PM.

  15. #15
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Originally posted by Edneeis

    The thing that screws it up the normal way is the syntax: redim Master(2)(3)

    But if you break out one element, expand it and put it back then it works.
    I posted this exact question a while ago. I wish you had seen it because this is exactly what I was trying to do. I think I eventually turned the second array into an object (array class) and then made the main array a collection. But now I can't remember what I was working on.

  16. #16
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Hmmm that kinda sucks because I usually read your posts, but I must have missed that one.

    Well better late than never I guess.

  17. #17
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    It's not too late. I'm saving your posts so I'll have them when I come across that code again. Then I'll check it against what I actually ended up doing. If it's better (probably is) then I'll convert mine.

  18. #18

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    8
    The thing that pisses me off more than anything else is that this would be a moot point if VB supported arrays in the same way that php supports them.
    And actually, I can think of several different purposes for a structure of this format. For instance, almost any threading model could benefit from the use of such a structure. I'm using it as a hash table for a tree structure that is stored in a database format to prevent excessive database hits.

    I had thought about the code that you suggest earlier Edneeis, but seeing how inefficent it was to do it that way killed the idea for me. heh.. and I had also thought about what you did cafeenman. Actually, that was my first solution to the problem but i thought, "no, there has got to be a better way than this."

    I've got a plan for a class that would allow one to implement arrays like php handles them, so i think ill write that class and then see where i can go from there.

  19. #19
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Originally posted by eat_static

    I've got a plan for a class that would allow one to implement arrays like php handles them, so i think ill write that class and then see where i can go from there.
    Check out my VB DB code. In the cTable class, there's an array property. It doesn't do exactly what you want, but if you spend 2 minutes stripping code from the class, you'll save yourself 20 minutes of typing the code that you can keep.

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