Posts: 272
Threads: 24
Joined: Apr 2022
Reputation:
59
04-10-2023, 08:46 PM
(This post was last modified: 04-10-2023, 10:07 PM by dbox.)
Starting a thread here to post fun facts about QBJS that you might not know.
Fun Fact #1 - You can define optional parameters!
Code: (Select All) PrintLines "First Test"
PrintLines "Second Test", 4
PrintLines "Third Test", , " -> "
Sub PrintLines (msg As String, lines As Integer, prefix As String)
If lines = undefined Then lines = 2 ' default to 2 lines
If prefix = undefined Then prefix = "" ' default to blank prefix
Dim i As Integer
For i = 1 To lines
Print prefix; msg
Next i
End Sub
Output:
Code: (Select All) First Test
First Test
Second Test
Second Test
Second Test
Second Test
-> Third Test
-> Third Test
Try it out on QBJS
Posts: 1,586
Threads: 59
Joined: Jul 2022
Reputation:
52
Posts: 54
Threads: 1
Joined: Apr 2022
Reputation:
7
04-10-2023, 10:23 PM
(This post was last modified: 06-05-2024, 08:57 AM by Sprezzo.)
------
Posts: 1,586
Threads: 59
Joined: Jul 2022
Reputation:
52
STRING$() function has weird behavior in QBJS. Is this on purpose?
Because I tried to test with the following code:
Code: (Select All) print string$(80, 42)
print string2$(80, 42)
print string$(40, "*+-")
print string2$(40, "*+-")
print string2$(40, "*+-", 2)
print string2$(20, "*+-", 3)
end
function string2$(quant as integer, astr as string, numchar as integer)
dim i as integer, sret$
if astr = undefined then astr = chr$(32)
if numchar = undefined then numchar = 1
for i = 1 to quant
sret$ = sret$ + left$(astr, numchar)
next
string2$ = sret$
end function
For the first line, I got "42" replicated 40 times. In QuickBASIC and "clones" it gives me 80 asterisks.
Even weirder, I do:
Code: (Select All) print string$(10, 432)
and the result is "4324324324". Not sure but it's supposed to flag an error that the second parameter must be type byte.
Otherwise this is pretty neat and I could finally shelf my REPEAT$(). LOL.
Posts: 272
Threads: 24
Joined: Apr 2022
Reputation:
59
(04-11-2023, 06:11 AM)mnrvovrfc Wrote: STRING$() function has weird behavior in QBJS. Is this on purpose?
Good catch! Looks like I missed the fact that the STRING$ function can take either a string or a character code. Fix will be included in the next release.
Posts: 272
Threads: 24
Joined: Apr 2022
Reputation:
59
Fun Fact #2 - Custom types can be returned from functions!
Code: (Select All) Screen 12
Type Circ
x As Integer
y As Integer
r As Integer
c As Integer
End Type
Dim i As Integer
Dim c As Circ
Do
Cls
For i = 1 To 100
c = MakeCircle
Circle (c.x, c.y), c.r, c.c
Next i
_Limit 10
Loop
Function MakeCircle
Dim c As Circ
c.x = Rnd * 640
c.y = Rnd * 480
c.r = Rnd * 100 + 10
c.c = Rnd * 14 + 1
MakeCircle = c
End Function
Try it on QBJS
Posts: 301
Threads: 16
Joined: Apr 2022
Reputation:
51
you can write things without brackets ie
Code: (Select All) cmul q, q, g
cdiv p, p, q
sub cmul( w(), z1(), z2() )
x1 = z1(0)
y1 = z1(1)
a1 = z2(0)
b1 = z2(1)
w(0) = x1*a1 - y1*b1
w(1) = x1*b1 + y1*a1
end sub
sub cdiv( w(), z1(), z2() )
x1 = z1(0)
y1 = z1(1)
a1 = z2(0)
b1 = z2(1)
d1 = a1*a1 + b1*b1
w(0) = (x1*a1 + y1*b1)/d1
w(1) = (y1*a1 - x1*b1)/d1
end sub
Posts: 272
Threads: 24
Joined: Apr 2022
Reputation:
59
Fun Fact #3 - QBJS has native support for associative arrays (dictionary).
This feature is sometimes also known as a map or hashmap.
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")
Try it on QBJS
Posts: 3,974
Threads: 177
Joined: Apr 2022
Reputation:
219
04-13-2023, 01:45 PM
(This post was last modified: 04-13-2023, 01:55 PM by bplus.)
Ah so that's how dictionary's are supposed to be used. I was wondering how I might apply the dictionary code I was working on yesterday. So I need to rename the Function that does a Value Lookup from a Key to something short and easy and maybe do a value conversion for numbers. Thanks for new ideas!
Now I wonder, could I do a whole program with variables and values from a dictionary? Bet I can! Be fun to try anyway
Ha! The JS part is really showing up the QB part ;-))
b = b + ...
Posts: 1,586
Threads: 59
Joined: Jul 2022
Reputation:
52
04-14-2023, 03:04 AM
(This post was last modified: 04-14-2023, 03:05 AM by mnrvovrfc.)
(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
|