Posts: 382
Threads: 56
Joined: Apr 2022
Reputation:
13
Has anyone head of a command "EXECUTE" in basic?? I have been trying to write a routine which will create a number of arrays with similar names however the name is to end with a unique number. After creating the arrays, each one is to have a difference upper limit. For example, the array names would look like - Array1(1 to 25), Array2(1 to 85), Array3(1 to 15) etc. I have tried this before and there is no way QB64PE can create array names on the fly using concatenation. Recently I was told I needed to use EXECUTE and this would create a new array name with a number concated to it. The example I was given is
EXECUTE "DIM SHARED DataHistory" + STR$(Number) + "(" + STR$(EVENT(Number)) + ") AS INTEGER"
Is Execute in some other basic syntax??
Posts: 3,964
Threads: 176
Joined: Apr 2022
Reputation:
219
Seems to me your source was thinking of some other BASIC.
b = b + ...
Posts: 1,272
Threads: 119
Joined: Apr 2022
Reputation:
100
The only time I have ever run across an EXECUTE command was in Minecraft (I think).
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Posts: 2,696
Threads: 327
Joined: Apr 2022
Reputation:
217
Sounds like a command which would work with an interpreter and not a compiler. Or a Steve(tm)-command from the days before we had a precompiler built-in, and I was writing code which had to be compiled via a QB64-PC.EXE which translated/expanded unique functions, before calling QB64 to finish the compilation.
If I recall correctly, @Dav even changed his IDE back at that time to add an option for "Precompile with:" just for me and some others to enjoy back then.
I don't think you'll find an EXECUTE command in any version of QB64 -- outside the odd chance you run into some of those QB64PC scripts from the late SDL days of the language.
Posts: 2,696
Threads: 327
Joined: Apr 2022
Reputation:
217
And one of the good things with these experimental concepts of Steve's(tm), is that many times, they either get added to the language itself eventually, or at least a portion of them do. Where all my custom precompiler commands started with '# back in the day, we now have $ commands for the precompiler -- $IF instead of my old '#IF and $LET instead of '#LET... I never got around to working in a $EXECUTE into the language, as the IDE we have built in translates and compiles code with each and every keystroke we make, checking syntax and other such stuff, which means it wouldn't make a singular call out to shell back to itself to execute some command and insert the output into the IDE once. Instead, it'd try to do that with EVERY keystroke, and you want to talk about a program lagging up and freezing almost instantly.... THAT'S one way to do it!
I know.... From Experience.
Posts: 3,964
Threads: 176
Joined: Apr 2022
Reputation:
219
01-03-2024, 03:31 AM
(This post was last modified: 01-03-2024, 03:34 AM by bplus.)
"Array1(1 to 25), Array2(1 to 85), Array3(1 to 15) etc. I have tried this before and there is no way QB64PE can create array names on the fly using concatenation."
You can do this without special array names or even multiple arrays.
I have lectured on this many times here that variable length strings can be made to function like arrays AND that to have sequence of variables ie var1, var2, var3, ...
Just use an array of variable length strings so
array$(1) = "1, 2, 3, 4, 5"
array$(2) = "1, 4, 9, 16, 25, 36"
array$(3) = "2, 4, 6"
array$(4) = "1, 1, 2, 4, 3, 9, 4, 16, 5, 25, 6, 36"
array$(5) = "a, b, c"
array$(6) = "dim, ster"
array$(7) = "b, p, l, us"
This extracts the item number from such a string:
Code: (Select All) Function Item$ (s$, nItem As Long, delimiter$)
Dim c As Long, d As Long, lastd As Long
If Len(s$) = 0 Then Item$ = "": Exit Function
lastd = 1: d = InStr(lastd, s$, delimiter$)
While d > 0
c = c + 1
If c = nItem Then
Item$ = Mid$(s$, lastd, d - lastd): Exit Function
Else
lastd = d + 1: d = InStr(lastd, s$, delimiter$)
End If
Wend
c = c + 1
If c <> nItem Then Item$ = "" Else Item$ = Mid$(s$, lastd, Len(s$))
End Function
The delimiter$ of the sample strings I gave is ", "
So all the 2nd items in all those strings that function like arrays are:
Code: (Select All) array$(1) = "1, 2, 3, 4, 5"
array$(2) = "1, 4, 9, 16, 25, 36"
array$(3) = "2, 4, 6"
array$(4) = "1, 1, 2, 4, 3, 9, 4, 16, 5, 25, 6, 36"
array$(5) = "a, b, c"
array$(6) = "dim, ster"
array$(7) = "b, p, l, us"
For i = 1 To 7
Print Item$(array$(i), 2, ", ")
Next
Function Item$ (s$, nItem As Long, delimiter$)
Dim c As Long, d As Long, lastd As Long
If Len(s$) = 0 Then Item$ = "": Exit Function
lastd = 1: d = InStr(lastd, s$, delimiter$)
While d > 0
c = c + 1
If c = nItem Then
Item$ = Mid$(s$, lastd, d - lastd): Exit Function
Else
lastd = d + 1: d = InStr(lastd, s$, delimiter$)
End If
Wend
c = c + 1
If c <> nItem Then Item$ = "" Else Item$ = Mid$(s$, lastd, Len(s$))
End Function
b = b + ...
Posts: 382
Threads: 56
Joined: Apr 2022
Reputation:
13
Thanks, I didn't think there was such a thing as Execute in the basic language anywhere. The arrays I was trying to create were not strings although I appreciate a variable/array name very much appears to be a string, altering the NON String array name using concatenation (reserved for joining two strings) doesn't work.
Pretty creative use of string arrays +b ... food for thought.
Posts: 3,964
Threads: 176
Joined: Apr 2022
Reputation:
219
And you only have to use Val(strNum$) to convert to a number.
All this string manipulation is very inefficient, but that doesn't mean dukey if all your arrays are running around 100 or less in items.
Look at all the time you save NOT managing a bunch of DIM'd arrays.
b = b + ...
Posts: 296
Threads: 10
Joined: Apr 2022
Reputation:
6
01-03-2024, 06:39 PM
(This post was last modified: 01-03-2024, 06:41 PM by aurel.)
Quote:Thanks, I didn't think there was such a thing as Execute in the basic language anywhere.
there was in Oxygen Basic and compile code in JIT and execute it of course
oups...it is called COMPILE ..not EXECUTE ..sorry
Posts: 296
Threads: 10
Joined: Apr 2022
Reputation:
6
and here is some explanation :
Quote:An Oxygen program can now compile strings of source code and run them without stopping. This allows EVAL functions to be implemented.
This is also a feature of Functional languages - being able to pass a function as a parameter of another function.
In our case, the function or expression is simply passed inside a string.
The compile() function is essentially a recursive operation and supports the full O2H Basic.
Compilefun3.tbasic
Code: (Select All) 'RUNTIME COMPILING FROM STRINGS
'
uses "oxygen"
dim src as string
src = "
'
global as double x,y,z
function evaluate (s as string, b as long, e as long) as string
dim a,p=1 as long
dim v,u,cr as string
cr=chr(13) chr(10)
v=nuls 4000
mid v,p,s+cr+cr
p+=4+len s
a=compile `o2h `+s
if len (error) then print `runtime error: ` error : frees a : exit function
for x=b to e
call a
u=str(x)+chr(9)+str(y)+chr(13)+chr(10)
mid v,p,u : p+=len u
next
function=left v,p-1
frees a
end function
print evaluate `y=x*x*x`,1,10
print evaluate `y=sqrt x`,1,9
"
o2_basic src
'msgbox 0, o2_prep "o2h "+src
if len(o2_error) then
msgbox 0, o2_error : stop
end if
o2_exec
|