LoginLogin

Getting variable name from string?

Root / Programming Questions / [.]

a_load_of_barnaclesCreated:
It doesn't reset the graphics or sprite page or anything of the sort. Places to store data..

arrays lol
Sure, but the whole point was to create scalar (single value) variables dynamically, right?

Sure, the variables are reset every exec, but if you call a COMMON DEF or something similar it will maintain state. Example:
'put in slot zero
EXEC 1
VALSET "VALUE",10
PRINT VALGET("VALUE")

'put in slot one
VAR VALUE
COMMON DEF VALSET N$,V
 VAR(N$)=V
END

COMMON DEF VALGET(N$)
 RETURN VAR(N$)
END

I believe you can declare variables across program slots using
VAR "(slot):(var name)"
, I may be wrong with the syntax there, but I know that it can be done.I imagine you could easily perform
VAR "0:"+INPUT$
, but for what context you intend to use this in I do not know, because if you're using the declared variable in the same slot, the variable will have to already be in the code.
You can't explicitly declare a slot global to be accessible to all, but you can refer to a slot global using functional var, example PRINT VAR("1:VARIABLE")

Anyway, it's way better and easier to just keep a table of named values in memory (string array probably) than it would be to constantly reallocate new variables. It can be done, but that doesn't mean it has to be done.

Ok, I made a system to dynamically create variables
DIM VARS$[0]
PRGEDIT 1
PRGDEL -1

DEF ADDVAR NAME$
 VAR I
 PRGDEL -1
 
 FOR I=0 TO LEN(VARS$)-1
  PRGINS "VAR "+VARS$[I]+"="+STR$(VAR("1:"+VARS$[I]))
 NEXT
 PRGINS "VAR "+NAME$
 PUSH VARS$,NAME$
 
 EXEC 1
END

Anyway, it's way better and easier to just keep a table of named values in memory (string array probably) than it would be to constantly reallocate new variables. It can be done, but that doesn't mean it has to be done.
Ok, I made a system to dynamically create variables
DIM VARS$[0]
PRGEDIT 1
PRGDEL -1

DEF ADDVAR NAME$
 VAR I
 PRGDEL -1
 
 FOR I=0 TO LEN(VARS$)-1
  PRGINS "VAR "+VARS$[I]+"="+STR$(VAR("1:"+VARS$[I]))
 NEXT
 PRGINS "VAR "+NAME$
 PUSH VARS$,NAME$
 
 EXEC 1
END
The former seems like the best choice for what I want to do, but I will compare them both later to see which is faster.

It's definitely better to use an array for this

If you're just storing strings, a new solution is at http://smilebasicsource.com/page?pid=702

If you're just storing strings, a new solution is at http://smilebasicsource.com/page?pid=702
>tfw when you make a dictionary library first but everyone ignores it Lol but yeah, dictionaries are a good alternative, and 12me's is much faster for small to medium data sets. And learning how to use a dictionary and how it works (which is a common data type in programming outside of languages like BASIC) is a good skill to have.

>tfw when you make a dictionary library first but everyone ignores it
Sorry I missed that. You could make better use of this opportunity, though, to make your library known, rather than just lament about how it is unknown. Show us!

>tfw when you make a dictionary library first but everyone ignores it
Sorry I missed that. You could make better use of this opportunity, though, to make your library known, rather than just lament about how it is unknown. Show us!
Lol I was being silly. Unfortunately, my dictionary library is mostly useless for data sets smaller than a couple hundred. Although performance most likely isn't an issue if you're willing to create dynamic variables on the fly, it's still a fact that my library is outperformed by 12me21's library by orders of magnitude for data sets smaller than around 100. If you need to use a dictionary with thousands of elements, mine might be better, but what the heck are you going to use 1000 element dictionaries for in SB? Anyway, I'm sorry for going off-topic. Don't worry about missing it; I made the library intentionally less noticeable because most of the things it does are done better by other people's libraries. I'm just stubborn and like making things for myself ^_^

you can make a function function

Here it is:
DEF CREATEVAR NAME, X
 PRGEDIT 1
 IF X*0 &&X*0==X*0 THEN X$ = CHR$(34) +X+CHR$(34) ELSE X$ = STR$(X)
 PRGINS FORMAT$("COMMON DEF %S() RETURN %S END",NAME,X$)
 EXEC 1'let SB compile it
 'Create a function(functions can work like variables, you know)
END
DEF GETVAR(NAME)
 IF CHKCALL(NAME) THEN RETURN CALL(NAME) ELSE RETURN ""
END

'CREATEVAR pseodo - code
'check type.
'if string, add quotes to the beginning and end so it returns the value as a string instead of a variable reference
'otherwise, turn into a string and insert it
'compile
'END
'GETVAR pseudo - code
'if function exists, return the value else return null ("")
'END
it would be a good idea to store all of the variable's names in a string array because the method I'm using would not be able to change variable values. the cool thing is you can also create arrays too. the hard part would be getting an index of it, though. GETVAR would return the array, but only the full array. You can make a filter to determine the input type, too. I might post on it later(I'm lazy)