|
Post by joan74 on Jul 1, 2020 17:22:50 GMT
Hi,
How make/Call a tab() with variable(s) plz ?
ty
'ie
'tab
dim tab0(12)
dim tab1(12)
dim tab2(12)
'init
tab0(6) = 0
tab1(6) = 11
tab2(6) = 22
'[...]
'---test 1
print selectTabV1(2) 'Go to select tab2() /This function is OK !
'---test 2
print selectTabV2(2) 'Go to select tab2() /Here BUG !!!
wait
'---------------------------------
function selectTabV1(x)
select case x
case 0
selectTabV1 = tab0(6)
case 1
selectTabV1 = tab1(6)
case 2
selectTabV1 = tab2(6)
end select
end function
'---------------------------------
function selectTabV2(x)
selectTabV2 = "tab";chr(x);"(";6;")"
end function
|
|
|
Post by cundo on Jul 1, 2020 21:29:10 GMT
Hi there Joan welcome to the forums. CHR$() is the function to convert a value represented in the ASCII table. STR$() converts a number value to a string.
|
|
|
Post by Rod on Jul 2, 2020 7:59:01 GMT
I dont think we can do what you want in Just BASIC. Liberty BASIC however has and eval() function that runs snippets of code and so allows a variable to mimic the literal value tab2(6)
'ie
'tab
dim tab0(12)
dim tab1(12)
dim tab2(12)
'init
tab0(6) = 0
tab1(6) = 11
tab2(6) = 22
'[...]
'---test 1
print selectTabV1(2) 'Go to select tab2() /This function is OK !
'---test 2
print selectTabV2(2) 'Go to select tab2() /Here BUG !!!
wait
'---------------------------------
function selectTabV1(x)
select case x
case 0
selectTabV1 = tab0(6)
case 1
selectTabV1 = tab1(6)
case 2
selectTabV1 = tab2(6)
end select
end function
'---------------------------------
function selectTabV2(x)
selectTabV2 = eval("tab";str$(x);"(6)")
end function
|
|
|
Post by joan74 on Jul 2, 2020 13:19:38 GMT
ty
|
|
|
Post by Rod on Jul 2, 2020 13:42:34 GMT
I think your first method is more understandable an easier to debug so perhaps all is not lost.
|
|
|
Post by honkytonk on Jul 2, 2020 15:21:25 GMT
I think Joan's question is: Can we pass an array as an argument?
|
|
|
Post by joan74 on Jul 2, 2020 17:00:06 GMT
I think your first method is more understandable an easier to debug so perhaps all is not lost. Right ! But if my goal is have more arg (arg1, arg2, arg3, etc) then SELECT CASE become bad way :/ select case arg1
case 1 select case arg2
case 1 [...] case 2
[...] case 3
[...] end select
case 2
select case arg2
case 1
[...]
case 2
[...]
case 3
[...]
end select
case 3 select case arg2
case 1
[...]
case 2
[...]
case 3
[...]
end select
end select
[etc etc etc ...]
Whereas, like this selectTabV2 = eval("tab";str$(arg1);"_";str$(arg2);"_";str$(arg3);"(6)") ... is just PERFECT... (imo!) ty again @++
|
|
|
Post by Rod on Jul 2, 2020 18:47:56 GMT
Ok, I think you are making the whole process WAY more complex than it need be. I have never seen anyone have to resort to mimicking variables to store data.
So you need to rethink and ask yourself how could I simplify this.
Just BASIC is really flexible and we surely don’t need to twist and distort it as you are doing.
In simple terms and without resorting to code describe what data you are storing and roughly how you want to access it.
|
|
|
Post by Rod on Jul 2, 2020 18:57:18 GMT
One complication is that you use the word tab() tab is a reserved word for tabbing text. So a change of name would be good. What does tab mean to you? Text position, is that what we are storing, an array of text positions?
|
|
|
Post by honkytonk on Jul 3, 2020 6:40:24 GMT
What is the final and general goal?
|
|
|
Post by joan74 on Jul 3, 2020 13:16:37 GMT
What is the final and general goal? For science ? More seriously, just tried upgrade my function... Make it more "versatile" Final goal is select a "good" file in deep folders (tree) with some args (... for my current game) Rodi used 'tab' like prefix for my array s names. i Will change it now (if reserved word)
|
|
|
Post by cundo on Jul 3, 2020 15:29:57 GMT
But what that function do? what's a tab (?)
|
|
|
Post by tsh73 on Jul 3, 2020 16:45:03 GMT
If you need to pass changing number of parameters, you could put them in a string pass a string inside, get values with WORD$ function You can convert string to number with VAL and do calculations on it
list$=" "
list$=list$+"First"+" " list$=list$+"Second"+" "
call processList list$ print call processList "1 2 10 5 six 22" print print print sumList("1 2 10") print print sumList("3 14 15 9 26") print print sumList("1 2 10 5 six 22") 'non-numbers vill VAL to 0 print
sub processList list$ print "Processing list >";list$;"<" item$="*" while 1 i = i+1 item$=word$(list$, i) if item$="" then exit while print "item " ;i;" ";item$ wend n = i-1 print "list has "; n ;" items" end sub
function sumList(list$) print "Processing list >";list$;"<" item$="*" while 1 i = i+1 item$=word$(list$, i) if item$="" then exit while sumList=sumList+val(item$) wend n = i-1 print "list has "; n ;" items" end function
Processing list > First Second < item 1 First item 2 Second list has 2 items
Processing list >1 2 10 5 six 22< item 1 1 item 2 2 item 3 10 item 4 5 item 5 six item 6 22 list has 6 items
Processing list >1 2 10< list has 3 items 13
Processing list >3 14 15 9 26< list has 5 items 67
Processing list >1 2 10 5 six 22< list has 6 items 40
|
|