Dynamic variable names in PureBasic?

Just starting out? Need help? Post your questions and find answers here.
phaselock.studio
User
User
Posts: 12
Joined: Wed Apr 13, 2022 10:08 pm
Location: Low Orbit

Dynamic variable names in PureBasic?

Post 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!
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Dynamic variable names in PureBasic?

Post 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.
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Dynamic variable names in PureBasic?

Post 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
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: Dynamic variable names in PureBasic?

Post 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?
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Dynamic variable names in PureBasic?

Post 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?
phaselock.studio
User
User
Posts: 12
Joined: Wed Apr 13, 2022 10:08 pm
Location: Low Orbit

Re: Dynamic variable names in PureBasic?

Post 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.
User avatar
mk-soft
Always Here
Always Here
Posts: 6207
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Dynamic variable names in PureBasic?

Post 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)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply