Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using one array to look up values in others

Status
Not open for further replies.

Shippwreck

Programmer
Oct 20, 2003
136
GB
Hi,

This is a question to which i have an answer but it is by far not the best. Here goes:

I want to use an one array to look up values in others, ie

Num1 = 0
Num2 = 0
Array1 = array("Pink", "Orange", "END")
Pink = array("1", "2", "3", "END")
Orange = array("4", "5", "6", "END")

Do until Target1 = "END"
Target1 = Array1(Num1)
*******

Do until Target2 = "END"
Target2 = Target1(Num2)
MsgBox Target2
Num2 = Num2 + 1
Loop

Num1 = Num1 + 1
Loop

Now the above is what i want to do except it doesn't work unless you add the following in where the stars are:

If Target1 = "Pink" then
Target1 = Pink
Else
If Target1 = "Orange" then
Target1 = Orange
End If
End If

When you put that in it works but if the first array is very large then its a hell of a lot of work to right out a huge long nested if (not to mention bad programming practice)

So if anyone has any ideas on how to sort this then please let me know.

Thanks for your help

 
I'm not sure exactly what you want to do. It looks like you want to have an array of named arrays. I would suggest looking at the Dictionary object. Then you could do something like this (untested code off the top of my head):
Code:
tempArray = array("1", "2", "3")
oDic.Add "Pink", tempArray
tempArray = array("4", "5", "6")
oDic.Add "Orange", tempArray
For Each strArray In oDic.Keys()
  For i = LBound(oDic(strArray)) To UBound(oDic(strArray))
    MsgBox oDic(srtArray)(i)
  Next
Loop

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
'im not sure what you actual goal is????

Set dicOrange = CreateObject("Scripting.Dictionary")
dicOrange.Add "1", "hello"
dicOrange.Add "2", "world"
dicOrange.Add "3", "merryxmas"

Set dicPurple = CreateObject("Scripting.Dictionary")
dicPurple.Add "4", "hello purple"
dicPurple.Add "5", "world p"
dicPurple.Add "6", "merry p"

Set dicTotal = CreateObject("Scripting.Dictionary")
dicTotal.Add "Orange", dicOrange
dicTotal.Add "Purple", dicPurple

Msgbox dicTotal.Item("Orange").Item("1")
For Each aKey In dicTotal
For Each anotherKey In dicTotal.Item(aKey)
Msgbox aKey & " " & anotherKey & " " & dicTotal.Item(aKey).Item(anotherKey)
Next
Next

If dicTotal.Exists("Orange") Then
Msgbox "we have an orange object!"
End If
 
hmmm, there seems to be some confusion over my goal.

The problem is that i have a program with a large number of arrays, 20-30. Now for different parts of this program to work i need to work through some of these arrays searching a document for the values in each array. The problem is that sometimes i will need to search for the values in arrays 1-10 and other times 11-20 or sometimes arrays 2,6,9,25. What i'm trying to say is that the arrays used in the search will change depending on who runs the program.

To this end i decided that the best way would be to dynamically create another array that would hold all the names of the other arrays to run in it. Then as the example shows the first array runs and sets off the second and the second is worked through, then it goes back to the first looks at the next element and then works through that array.

Below is a working piece of code that does what i want (except it displays the elements in the array to keep the code short), just copy and paste.

Sub Test()

Num1 = 0
Array1 = Array("Pink", "Orange", "END")
Pink = Array("1", "2", "3", "END")
Orange = Array("4", "5", "6", "END")

Do
Target1 = Array1(Num1)

If Target1 = "END" Then
Exit Do
End If

If Target1 = "Pink" Then
Target1 = Pink
Else
If Target1 = "Orange" Then
Target1 = Orange
End If
End If
Num2 = 0
Do
Target2 = Target1(Num2)

If Target2 = "END" Then
Exit Do
End If

MsgBox Target2
Num2 = Num2 + 1
Loop

Num1 = Num1 + 1
Loop

End Sub

 
Also, i haven't had a chance to look at the two solutions posted yet but i would really like to use arrays for this as the same arrays are used elsewhere in the program so they would all need to be in the code anyway.

 
If you are dead set against using a better data structure, then the only real enhancement that I can suggest would be to use a Select...Case structure instead of a bunch of If...Then statements.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top