QBJS - Fun Facts!
#14
(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

   
Reply


Messages In This Thread
QBJS - Fun Facts! - by dbox - 04-10-2023, 08:46 PM
RE: QBJS - Fun Facts! - by mnrvovrfc - 04-10-2023, 09:48 PM
RE: QBJS - Fun Facts! - by Sprezzo - 04-10-2023, 10:23 PM
RE: QBJS - Fun Facts! - by mnrvovrfc - 04-11-2023, 06:11 AM
RE: QBJS - Fun Facts! - by dbox - 04-11-2023, 08:55 PM
RE: QBJS - Fun Facts! - by dbox - 04-11-2023, 09:20 PM
RE: QBJS - Fun Facts! - by vince - 04-11-2023, 10:03 PM
RE: QBJS - Fun Facts! - by dbox - 04-13-2023, 11:48 AM
RE: QBJS - Fun Facts! - by mnrvovrfc - 04-14-2023, 03:04 AM
RE: QBJS - Fun Facts! - by CharlieJV - 04-14-2023, 04:00 AM
RE: QBJS - Fun Facts! - by bplus - 04-13-2023, 01:45 PM
RE: QBJS - Fun Facts! - by SMcNeill - 04-14-2023, 11:38 AM
RE: QBJS - Fun Facts! - by dbox - 04-14-2023, 01:31 PM
RE: QBJS - Fun Facts! - by vince - 04-14-2023, 11:55 AM
RE: QBJS - Fun Facts! - by mnrvovrfc - 04-14-2023, 02:53 PM
RE: QBJS - Fun Facts! - by dbox - 04-14-2023, 03:55 PM
RE: QBJS - Fun Facts! - by dbox - 04-21-2023, 02:04 PM



Users browsing this thread: 4 Guest(s)