(04-14-2023, 11:38 AM)SMcNeill Wrote: I'd think, for speed and readability, I'd just use a function with select case.
...
Just as fast as the IF...ELSEIF... version. Uses almost the same amount of coding as the qbjs version does to define the dictionary, and is just as easily readable.
I think perhaps my first example was too simplistic. It was just intended to show the syntax for how the associative array (or dictionary) could be used in QBJS. If the use case really only had three colors, I would just define variables for each.
Here's a more fleshed out example. Say you want to read in a flat file containing client information, and you want to be able to lookup a client by their account number. An associative array could make that exercise pretty easy:
Code: (Select All)
Type Account
acctnum As String
name As String
phone As String
email As String
End Type
Dim Shared accounts() As Account
CreateDataFile
ReadDataFile
Dim search As String
Dim acct As Account
Print "Client Search 2000"
Print "----------------------------------------------------"
Do
Input "Enter a policy number (Q to quit): ", search
If search = "Q" or search = "q" Then
Print "Thank you for using Client Search 2000"
System
End If
acct = accounts(search)
If acct.acctnum = "" Then
Print "No account found with account #: " + search
Else
Print "Account #: " + acct.acctnum
Print "Name: " + acct.name
Print "Phone: " + acct.phone
Print "Email: " + acct.email
End If
Print
Print "----------------------------------------------------"
Loop
Print
Sub ReadDataFile
Open "data.csv" For Input As #1
While Not EOF(1)
Dim a As Account
Input #1, a.acctnum, a.name, a.phone, a.email
accounts(a.acctnum) = a
Wend
Close #1
End Sub
' just for demonstration purposes, idea here is that you would
' read from a much larger pre-existing data file
Sub CreateDataFile
Open "data.csv" For Output As #1
Write #1, "KW577534", "Victoria Armstrong", "(776) 742-8626", "semper.nam@hotmail.ca"
Write #1, "WK954847", "Karly Duran", "(380) 391-1761", "aliquet@outlook.org"
Write #1, "SY322021", "Lyle Emerson", "1-213-358-1418", "nisl.nulla@hotmail.edu"
Write #1, "SP427214", "Nigel Turner", "1-563-968-0564", "risus@yahoo.com"
Write #1, "UR543977", "Eve Taylor", "1-176-484-8687", "fringilla@protonmail.org"
Close #1
End Sub
Try it on QBJS