How to access array of arrays

I'm trying to speed up data access by getting all the data I need from a CD when my VB6 app starts.

A stock price function I use, which is from a 3rd party, returns the symbol and a bunch of arrays for date & price data.  The problem is that it accesses the CD every time it executes so it's rather slow.

Can anyone suggest a way for me to put everything in an array or collection (or something else?) for easy access?

I tried putting the data in an array of arrays -one for each symbol- but I couldn't figure out a way to access the data.

Thanks,

Francis Gingras
fgingrasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

InteqamCommented:
MsgBox Array1[Index1][Index2]
0
KingSunCommented:
Would u pls explain it more clearly by using some examples.

my first feeling is that you can use type & array like below.

type symbol_detail
    date as date
    price as double
end type

type myNode
     Symbol_ID as long
     myData() as symbol_detail
end type

private myCollection() as mynode

using myCollection to store all data, for each element, have 1 Symbol_ID as key, and all detail data stored in mydata.

To change array length, just use redim, like:
redim mycollection(100)
redim mycollection(1).mydata(5)...

0
mcriderCommented:
Here is a way to create a "hashed array" in VB... A hashed array is nothing more than a sorted array with a key.

For example, let's say that you have a trading symbol called "IBM" and it has a value of "20"... Put the two elements together in a string like this "IBM:20", then you can add this string to an array. You can then use the following code to get the value "20" from the array.


Add the following code to a MODULE in a new project:

'-----------------------------------------------
    Function DoesHashExist(InArray As Variant, Hash As String) As Boolean
        Dim hStart As Long
        Dim hEnd As Long
        Dim hIndex As Long
        Dim hElement As String
       
        DoesHashExist = False
        hStart = LBound(InArray)
        hEnd = UBound(InArray)
        hIndex = Fix(UBound(InArray) / 2)
        Do
            hElement = GetField(CStr(InArray(hIndex)), ":", 1)
            If hElement = Hash Then
                DoesHashExist = True
                Exit Do
            End If
            If Hash > hElement Then
                hStart = hIndex + 1
                hIndex = hStart + Fix((hEnd - hStart) / 2)
            Else
                hEnd = hIndex - 1
                hIndex = hStart + Fix((hEnd - hStart) / 2)
            End If
            If hIndex > hEnd Then
                hIndex = -1
                Exit Do
            End If
        Loop
    End Function
    Function GetArrayHash(InArray As Variant, Hash As String) As String
        Dim hStart As Long
        Dim hEnd As Long
        Dim hIndex As Long
        Dim hElement As String
       
        hStart = LBound(InArray)
        hEnd = UBound(InArray)
        hIndex = Fix(UBound(InArray) / 2)
        Do
            hElement = GetField(CStr(InArray(hIndex)), ":", 1)
            If hElement = Hash Then Exit Do
            If Hash > hElement Then
                hStart = hIndex + 1
                hIndex = hStart + Fix((hEnd - hStart) / 2)
            Else
                hEnd = hIndex - 1
                hIndex = hStart + Fix((hEnd - hStart) / 2)
            End If
            If hIndex > hEnd Then
                hIndex = -1
                Exit Do
            End If
        Loop
        On Error Resume Next
        GetArrayHash = GetField(CStr(InArray(hIndex)), ":", 2)
    End Function
    Function GetField(Source As String, Delimiter As String, Optional Position As Variant) As String
        'THIS FUNCTION RETURNS THE SPECIFIED FIELD IN A STRING
        Dim iVal As Integer
        Dim jVal As Integer
        Dim kVal As Integer
        Dim tBuf As String
        jVal = 0: kVal = 1
        tBuf = Source + Delimiter
        If IsMissing(Position) = False Then
            For iVal = 1 To Val(Position) - 1
                jVal = InStr(jVal + 1, tBuf, Delimiter)
                If jVal = 0 Then
                    GetField = ""
                    Exit Function
                End If
            Next iVal
            kVal = jVal + 1
        End If
        iVal = InStr(jVal + 1, tBuf, Delimiter)
        If iVal = 0 Then
            GetField = ""
        Else
            GetField = Mid$(tBuf, kVal, iVal - kVal)
        End If
    End Function
    Sub SortArray(InArray As Variant, FieldNumber As Long, Delimit As String)
        'THIS FUNCTION SORTS AN ARRAY BASED ON THE FIELDNUMBER IN THE ARRAY
        Dim lLoop1 As Long
        Dim lLoop2 As Long
        Dim tBuf As String
        For lLoop1 = UBound(InArray) To LBound(InArray) Step -1
            For lLoop2 = LBound(InArray) + 1 To lLoop1
                If GetField(CStr(InArray(lLoop2 - 1)), Delimit, FieldNumber) > _
                    GetField(CStr(InArray(lLoop2)), Delimit, FieldNumber) Then
                    tBuf = InArray(lLoop2 - 1)
                    InArray(lLoop2 - 1) = InArray(lLoop2)
                    InArray(lLoop2) = tBuf
                End If
            Next lLoop2
        Next lLoop1
    End Sub
'-----------------------------------------------


The following code uses the above functions.  In this example, I am using the ARRAY function to quickly create an array.  This is just for the example.  You would have to read your data and put it into the array:


        Dim X As Variant
        Dim Y As String
        Dim iVal As Long
       
      'BUILD THE ARRAY
        X = Array("MSX:10", "IBM:20", "USX:30", "QWERTY:40", _
            "INT:50", "XXX:60", "YYY:70", "ZZZ:80", _
            "AAA:90", "BBB:100", "CCC:110", "DDD:120", _
            "EEE:130", "FFF:140", "GGG:150", "HHH:160", _
            "III:170", "JJJ:180", "KKK:190", "LLL:200", _
            "MMM:210", "NNN:220", "OOO:230", "PPP:240", _
            "QQQ:250", "RRR:260", "SSS:270", "TTT:280", _
            "UUU:290", "VVV:300", "WWW:310", "X1X:320", _
            "Y1Y:330", "Z1Z:340", "A1A:350", "A1B:360")

      'SORT THE ARRAY ON THE FIRST FIELD DELIMITED BY ":"
        SortArray X, 1, ":"

        '------------------------------------------
      'THIS CODE WILL FIND THE HASH FOR "IBM"
        'AND DISPLAY "20" IN THE DEBUG WINDOW
        If DoesHashExist(X, "IBM") = True Then
            Y = GetArrayHash(X, "IBM")
            Debug.Print Y
        Else
            Debug.Print "HASH DOESN'T EXIST"
        End If

      '------------------------------------------
      'THIS CODE WILL TRY TO FIND A NON-EXISTANT
        'HASH AND DISPLAY "HASH DOESN'T EXIST"
      'IN THE DEBUG WINDOW
        If DoesHashExist(X, "MICROSOFT") = True Then
            Y = GetArrayHash(X, "MICROSOFT")
            Debug.Print Y
        Else
            Debug.Print "HASH DOESN'T EXIST"
        End If



Cheers!
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

fgingrasAuthor Commented:
Here's an example that shows the data returned from the function:

Dim o() As Single, h() As Single, l() As Single, c() As Single
Dim Dates() As Long, StockNb As Long, Vol() As Long

'Base 1 Arrays
SFServ.GetPrices StockNb, o(), h(), l(), c(), Vol(), Dates()


As you can see, I get a few arrays and one string symbol.

I fail to see how I could use the (very nice) hashed array example by mcrider since I need to store arrays of price and date data.

The comment by KingSun seems closer to something I can use but how would I store all 6 arrays into symbol_detail without having to loop every array from LBound to UBound?

And how to retrieve say h(95) from the symbol "IBM"?  Is it MyCollection(IBM).MyData(95).High?

Thanks for the answers,

Francis
0
TheAnswerManCommented:
type myType
     strSymbol as Long
     asi_Open() as Single
     asi_High() as Single
     asi_Low() as Single
     asi_Close() as Single
     al_Dates() as Long
end type

Dim eta_Stocks(1 to 1) as MyType
'this example only one

With eta_Stocks(1)
   .strSymbol = "IBM"
   SFServ.GetPrices .strSymbol , .asi_Open(), .asi_High(), .asi_Low(), .asi_Close(), .asi_Vol(), .asi_Dates()
End With

....or you could call it this way.. but it is slightly slower
eta_Stocks(1).strSymbol = "IBM"
SFServ.GetPrices eta_Stocks(1).strSymbol , eta_Stocks(1).asi_Open(), vasi_High(), vasi_Low(), eta_Stocks(1).asi_Close(), eta_Stocks(1).asi_Vol(), eta_Stocks(1).asi_Dates()


In that example.. you would have to know the Index of the Symbol IBM.. in this case 1.

You would get it like this
MsgBox eta_Stocks(1).High(95)

If you store all of the Symbols in an array, you could write a little function to get the index.. and you could call like this
MsgBox eta_Stocks(GetIndex("IBM")).High(95)

'' in this example assume your function called GetIndex would return a value of 1.


Otherwise you would do an actual collection instead of an array of types.
If you did that you could get it like this..
MsgBox eta_Stocks("IBM").High(95)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fgingrasAuthor Commented:
Wonderful, thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.