Page 1 of 1

Dynamic variable names in PureBasic?

Posted: Tue Apr 19, 2022 3:18 am
by phaselock.studio
What I mean is where the variable names are generated based on something like a counter. Something like this:

Code: Select all

For i = 1 To 10
  var_ + i = i
Next
Which would create 10 variables:

Code: Select all

var_1 = 1
var_2 = 2 
...
var_10 = 10
Is this possible? Thanks!

Re: Dynamic variable names in PureBasic?

Posted: Tue Apr 19, 2022 3:34 am
by Tenaja
No, you can't do that with a for loop, but you can do it with macros. They are pretty useful.

The only downside is you have to run the macro for each new variable.

Re: Dynamic variable names in PureBasic?

Posted: Tue Apr 19, 2022 5:58 am
by STARGĂ…TE
Yes, you can use Runtime:

Code: Select all

Define var1, var2, var3

Runtime var1, var2, var3

For i = 1 To 3
  SetRuntimeInteger("var"+i, i)
Next

Debug var1
Debug var2
Debug var3

Re: Dynamic variable names in PureBasic?

Posted: Tue Apr 19, 2022 7:11 am
by IceSoft
phaselock.studio wrote: Tue Apr 19, 2022 3:18 am What I mean is where the variable names are generated based on something like a counter. Something like this:

Code: Select all

For i = 1 To 10
  var_ + i = i
Next
Which would create 10 variables:

Code: Select all

var_1 = 1
var_2 = 2 
...
var_10 = 10
Is this possible? Thanks!

I never understand the sense of this kind of programing.
Why not using arrays?

Re: Dynamic variable names in PureBasic?

Posted: Tue Apr 19, 2022 1:17 pm
by Tenaja

Code: Select all

Yes, you can use Runtime
Wow, that one slipped by me in all of the module excitement!

But why not use an array, as icesoft said?

Re: Dynamic variable names in PureBasic?

Posted: Tue Apr 19, 2022 3:18 pm
by phaselock.studio
Thanks everyone for the replies, these are great!

I was trying to figure out a way to initialize a variable number of nested Lists/Arrays that are supplied by argument to a Procedure, and could be referenced after creation (by the generated variable names). I could not figure out how to do this except for using a For loop to initialize the Lists/Arrays where the iteration # of the loop is appended to a common variable name.

I'm probably thinking about this all wrong, so if you have any suggestions they would be very much appreciated.

Re: Dynamic variable names in PureBasic?

Posted: Tue Apr 19, 2022 3:38 pm
by mk-soft
Maybe start with structures and combine the required variables in them.

Code: Select all

;-TOP

Structure sDataSet
  iVal1.i
  iVal2.i
  fltVal.f
  sVal.s
EndStructure

Procedure InitDataSet(*Data.sDataSet)
  *Data\iVal1 = 1
  *Data\iVal2 = 2
  *Data\fltVal = 0.0
  *Data\sVal = "Hello World"
EndProcedure


Procedure AddDataSet(*Data.sDataSet, iVal1, iVal2)
  *Data\iVal1 + iVal1
  *Data\iVal2 + iVal2
  *Data\fltVal = *Data\iVal1 * *Data\iVal2
  *Data\sVal = "Result = " + StrF(*Data\fltVal, 2)
EndProcedure


Procedure OutputDataSet(*Data.sDataSet)
  With *Data
    Debug "iVal1 = " + \iVal1
    Debug "iVal2 = " + \iVal2
    Debug "fltVal = " + \fltVal
    Debug "sVal = " + \sVal
  EndWith
EndProcedure

Global MyData1.sDataSet

InitDataSet(@MyData1)
OutputDataSet(@MyData1)
AddDataSet(@MyData1, 10, 5)
OutputDataSet(@MyData1)