|
-
Jul 10th, 2002, 12:38 PM
#1
Thread Starter
New Member
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.
-
Jul 10th, 2002, 01:11 PM
#2
Hyperactive Member
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)
-
Jul 10th, 2002, 01:16 PM
#3
Hyperactive Member
please tell me if that works for you. I hope that it helped
-
Jul 10th, 2002, 01:17 PM
#4
You could use a variant, which can hold an array.
VB Code:
Dim Master(1) As Variant
Dim Element1 As Variant
Dim Element2 As Variant
Element1 = Array("Ed", "Cookie", "Mike")
Element2 = Array("Admin", "VP", "DQ")
Master(0) = Element1
Master(1) = Element2
MsgBox Master(0)(1) 'shows cookie
-
Jul 10th, 2002, 01:17 PM
#5
Thread Starter
New Member
im trying it as we speak
-
Jul 10th, 2002, 01:17 PM
#6
Damn I type too slow. Although I did test it and it works.
-
Jul 10th, 2002, 04:55 PM
#7
Thread Starter
New Member
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"] ?
-
Jul 10th, 2002, 05:46 PM
#8
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:
Dim Master() As Variant
Dim Element1 As Variant
Dim Element2 As Variant
Dim Element3 as Variant
Element1 = Array("Ed", "Cookie", "Mike")
Element2 = Array("Admin", "VP", "DQ")
Element3 = Array(25, 49, 26)
ReDim Master(1)
Master(0) = Element1
Master(1) = Element2
MsgBox Master(0)(1) 'shows cookie
'extend array
ReDim Preserve Master(2)
Master(2)=Element3
MsgBox Master(2)(0) 'shows 25
-
Jul 10th, 2002, 06:47 PM
#9
Thread Starter
New Member
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
-
Jul 10th, 2002, 07:15 PM
#10
PowerPoster
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()()
-
Jul 10th, 2002, 07:17 PM
#11
Isn't this the same as a 2D array?
Code:
Dim MyArray(10, 10) As String
-
Jul 10th, 2002, 07:19 PM
#12
PowerPoster
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.
-
Jul 10th, 2002, 09:37 PM
#13
You just have to be tricky with it, but it can be done look:
VB Code:
Dim Master() As Variant
Dim Element1 As Variant
Dim Element2 As Variant
Dim Element3 As Variant
Element1 = Array("Ed", "Cookie", "Mike")
Element2 = Array("Admin", "VP", "DQ")
Element3 = Array(25, 49, 26)
ReDim Master(1)
Master(0) = Element1
Master(1) = Element2
MsgBox Master(0)(1) 'shows cookie
'extend array
ReDim Preserve Master(2)
Master(2) = Element3
MsgBox Master(2)(0) 'shows 25
ReDim Preserve Element3(3)
Element3(3) = "Extra"
Master(2) = Element3
MsgBox Master(2)(3) 'shows extra but still holds the values that were there before
'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.
-
Jul 10th, 2002, 09:41 PM
#14
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:
Dim Master() As Variant
Private Sub Command1_Click()
Dim Element1 As Variant
Dim Element2 As Variant
Dim Element3 As Variant
Element1 = Array("Ed", "Cookie", "Mike")
Element2 = Array("Admin", "VP", "DQ")
Element3 = Array(25, 49, 26)
ReDim Master(1)
Master(0) = Element1
Master(1) = Element2
MsgBox Master(0)(1) 'shows cookie
'extend array
ReDim Preserve Master(2)
Master(2) = Element3
MsgBox Master(2)(0) 'shows 25
End Sub
Private Sub Command2_Click()
Dim Element3 As Variant
Element3 = Master(2)
ReDim Preserve Element3(3)
Element3(3) = "Extra"
Master(2) = Element3
MsgBox Master(2)(3) 'shows extra
MsgBox Master(2)(1) 'make sure it still retained the previous values
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.
-
Jul 10th, 2002, 09:41 PM
#15
PowerPoster
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.
-
Jul 10th, 2002, 09:46 PM
#16
Hmmm that kinda sucks because I usually read your posts, but I must have missed that one.
Well better late than never I guess.
-
Jul 10th, 2002, 09:50 PM
#17
PowerPoster
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.
-
Jul 10th, 2002, 11:25 PM
#18
Thread Starter
New Member
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.
-
Jul 10th, 2002, 11:27 PM
#19
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|