Posts: 597
Threads: 110
Joined: Apr 2022
Reputation:
34
(04-14-2023, 03:04 AM)mnrvovrfc Wrote: (04-13-2023, 11:48 AM)dbox Wrote: Code: (Select All) Dim colors() As _Unsigned Long
colors("brick red") = &HFFC62D42
colors("electric lime") = &HFFCCFF00
colors("metalic sunburst") = &HFF9C7C38
Cls , 15
Line (10, 10)-(100, 100), colors("electric lime"), BF
Line (200, 200)-(300, 300), colors("brick red"), BF
Circle (200, 100), 50, colors("metalic sunburst")
This could be faked as function, and on 64-bit it's faster than any "dictionary" implementation:
Code: (Select All) FUNCTION colors~& (mycolor$)
DIM iret AS _UNSIGNED LONG
IF mycolor$ = "brick red" THEN
iret = &HFFC62D42
ELSEIF mycolor$ = "electric lime" THEN
iret = &HFFCCFF00
ELSEIF mycolor$ = "metallic sunburst" THEN
iret = &HFF9C7C38
ELSE
iret = 0
END IF
colors~& = iret
END FUNCTION
Sure, but the "dictionary" code is way easier to read, and much better for filling the dictionary with values coming from DATA statements or from files. The data source, as-is, works as documentation.
If speed is critical, then pick the speedy one. If flexibility and readability are critical, then go for the dictionary.
Posts: 2,697
Threads: 327
Joined: Apr 2022
Reputation:
217
04-14-2023, 11:38 AM
(This post was last modified: 04-14-2023, 11:39 AM by SMcNeill.)
I'd think, for speed and readability, I'd just use a function with select case.
Code: (Select All) FUNCTION colors~& (mycolor$)
SELECT CASE LCASE$(mycolor$)
CASE "brick red": colors = &HFFC62D42
CASE "electric lime": colors = &HFFCCFF00
CASE "metallic sunburst": colors = &HFF9C7C38
END SELECT
END FUNCTION
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.
Posts: 301
Threads: 16
Joined: Apr 2022
Reputation:
51
04-14-2023, 11:55 AM
(This post was last modified: 04-14-2023, 12:01 PM by vince.)
Some more fun facts
if you wish to run QBJS completely offline and for it to appear like a native program you can do so by:
- downloading the QBJS project from github to your local hard drive
- if you are using chrome/chromium you can use the --app="~/qbjs-master/index.html" command-line option which will start the browser with none of the 'browsing' features or decorations so it appears just like a native app
- and while javascript browser programs may not reach the performance of native compiled C++ in some circumstances, the browser QBJS IDE is, in fact, much faster and better performing than QB64's (mainly due to the incredibly poor implementation of the multiline textbox in QB64's default IDE)
Same can be said for smartphones/tablets, ie Android and iPhone devices: either place the QBJS master folder in your internal storage and simply upload .BAS files or use QBJS's export feature and upload the prepackaged html/javascript and run it with your device's browser with touchscreen and full screen support for a seamless app experience while also conveniently bypassing any of app store or developer mode layers
If you are indeed concerned about javascript's performance or the capability of your smartphone apps to directly access the devices hardware (ie bluetooth, camera, etc) then you are very far lost to be considering a QBasic based language/compiler
Posts: 272
Threads: 24
Joined: Apr 2022
Reputation:
59
04-14-2023, 01:31 PM
(This post was last modified: 04-14-2023, 03:53 PM by dbox.)
(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
Posts: 1,586
Threads: 59
Joined: Jul 2022
Reputation:
52
04-14-2023, 02:53 PM
(This post was last modified: 04-14-2023, 02:55 PM by mnrvovrfc.)
Put it into string arrays and search by field? Maybe not sequentially?
Besides the output shown is wrong. For the requested policy number the user should be "Victoria Armstrong". Unless those are scrambled in a certain way not shown obviously by the program. Even running this thing live displays the same thing. Also putting down "Lyle's" account number gives me "Eve" instead.
Before you guys keep pelting me, I recognize associative arrays are useful because I also program in Lua.
Posts: 272
Threads: 24
Joined: Apr 2022
Reputation:
59
(04-14-2023, 02:53 PM)mnrvovrfc Wrote: Put it into string arrays and search by field? Maybe not sequentially?
Besides the output shown is wrong. For the requested policy number the user should be "Victoria Armstrong". Unless those are scrambled in a certain way not shown obviously by the program. Even running this thing live displays the same thing. Also putting down "Lyle's" account number gives me "Eve" instead.
Before you guys keep pelting me, I recognize associative arrays are useful because I also program in Lua.
Another good catch @mnrvovrfc! Apparently, I put that together too fast. It matters where you Dim your variables. I've updated the original post with the corrected version.
Posts: 272
Threads: 24
Joined: Apr 2022
Reputation:
59
Fun Fact #4 - You can call native Javascript from your QBJS program!
Here's a simple example that utilizes the browser's text-to-speech functionality:
Code: (Select All) Dim s As String
Print "What would you like me to say?"
Line Input s
Say s
Sub Say (text As String)
Dim success As Integer
$If Javascript Then
if (window.speechSynthesis) {
var utterance = new SpeechSynthesisUtterance(text);
window.speechSynthesis.speak(utterance);
success = -1;
}
$End If
If Not success Then
Print "Sorry, this is not supported in your browser."
End If
End Sub
Try it on QBJS!
Posts: 272
Threads: 24
Joined: Apr 2022
Reputation:
59
01-31-2024, 03:53 PM
(This post was last modified: 01-31-2024, 07:57 PM by dbox.)
Fun Fact #5 - _LoadImage can load images directly from a URL!
|