Just starting out? Need help? Post your questions and find answers here.
phaselock.studio
User
Posts: 12 Joined: Wed Apr 13, 2022 10:08 pm
Location: Low Orbit
Post
by phaselock.studio » 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:
Which would create 10 variables:
Code: Select all
var_1 = 1
var_2 = 2
...
var_10 = 10
Is this possible? Thanks!
Tenaja
Addict
Posts: 1959 Joined: Tue Nov 09, 2010 10:15 pm
Post
by Tenaja » Tue Apr 19, 2022 3:34 am
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.
STARGÅTE
Addict
Posts: 2227 Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:
Post
by STARGÅTE » Tue Apr 19, 2022 5:58 am
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
IceSoft
Addict
Posts: 1682 Joined: Thu Jun 24, 2004 8:51 am
Location: Germany
Post
by IceSoft » Tue Apr 19, 2022 7:11 am
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:
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,...
Tenaja
Addict
Posts: 1959 Joined: Tue Nov 09, 2010 10:15 pm
Post
by Tenaja » Tue Apr 19, 2022 1:17 pm
Wow, that one slipped by me in all of the module excitement!
But why not use an array, as icesoft said?
phaselock.studio
User
Posts: 12 Joined: Wed Apr 13, 2022 10:08 pm
Location: Low Orbit
Post
by phaselock.studio » Tue Apr 19, 2022 3:18 pm
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.
mk-soft
Always Here
Posts: 6207 Joined: Fri May 12, 2006 6:51 pm
Location: Germany
Post
by mk-soft » Tue Apr 19, 2022 3:38 pm
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)