Is it possible to use array cell contents as variable names
Is it possible to use array cell contents as variable names
Let's use a simple one cell array. I need to check the variable whose name is in the cell. For example, the string in the cell is "mileage" , which is also a variable name. Whatever variable name is in the cell is the one I need to get the value for.
Is it possible for PB to to get or set the value of a variable name based on the name it finds in the array cell WITHOUT using select-case?
So if I change the contents of the cell, which is a string, to "distance" , PB will use the distance variable instead of mileage. Will it have to be done with pointers?
If you dont understand the question, let me try it this way: I have a function that will change the value of the variable name found in an array cell, and the function will apply it to whatever variable name it finds. There may be hundreds of possible variable names, so we don't want to use select-case. We just want to use the variable name it finds in the array cell as the function parameter.
Is this possible with PB or even C+?
Is it possible for PB to to get or set the value of a variable name based on the name it finds in the array cell WITHOUT using select-case?
So if I change the contents of the cell, which is a string, to "distance" , PB will use the distance variable instead of mileage. Will it have to be done with pointers?
If you dont understand the question, let me try it this way: I have a function that will change the value of the variable name found in an array cell, and the function will apply it to whatever variable name it finds. There may be hundreds of possible variable names, so we don't want to use select-case. We just want to use the variable name it finds in the array cell as the function parameter.
Is this possible with PB or even C+?
Re: Is it possible to use array cell contents as variable names
Did you consider using a Map()?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Re: Is it possible to use array cell contents as variable names
Harbinger wrote: Wed Dec 24, 2025 4:13 am... I need to check the variable whose name is in the cell. For example, the string in the cell is "mileage" , which is also a variable name. Whatever variable name is in the cell is the one I need to get the value for.
Is it possible for PB to to get or set the value of a variable name based on the name it finds in the array cell WITHOUT using select-case?
So if I change the contents of the cell, which is a string, to "distance" , PB will use the distance variable instead of mileage. Will it have to be done with pointers?
If understood correctly, the Runtime function should do the trick:
Code: Select all
EnableExplicit
Define event, appQuit, row, vehicleChanged, selectedVariableName.s
Define.s name, vehicle, mileage, distance
Runtime name, vehicle, mileage, distance
name = "John"
vehicle = "Ford"
mileage = "25,000"
distance = "1,200"
If OpenWindow(0, 0, 0, 200, 150, "Runtime Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ListIconGadget(0, 5, 5, 190, 140, "Variables", 190, #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection)
AddGadgetItem(0, -1, "name")
AddGadgetItem(0, -1, "vehicle")
AddGadgetItem(0, -1, "mileage")
Repeat
event = WaitWindowEvent()
Select event
Case #PB_Event_CloseWindow
appQuit = #True
Case #PB_Event_Gadget
If EventGadget() = 0 And EventType() = #PB_EventType_LeftClick
row = GetGadgetState(0)
selectedVariableName = GetGadgetItemText(0, row)
Debug "The value for the variable [" + selectedVariableName + "] is '" + GetRuntimeString(selectedVariableName) + "'."
If selectedVariableName = "vehicle" And Not vehicleChanged
SetRuntimeString(selectedVariableName, "BMW")
Debug "The value for the variable [" + selectedVariableName + "] has been changed to 'BMW'."
vehicleChanged = #True
ElseIf selectedVariableName = "mileage"
SetGadgetItemText(0, row, "distance")
Debug "The variable in the third row cell has been changed to [distance]."
EndIf
EndIf
EndSelect
Until appQuit
EndIf
Hope it helps.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel 
Re: Is it possible to use array cell contents as variable names
Instead of creating hundreds of variables, you can also work with structures and index.
Here is an example with an array and a map as an index.
Here is an example with an array and a map as an index.
Code: Select all
;-TOP
Structure udtVariable
Name.s
Value.s
EndStructure
; Array
Global Dim Variable.udtVariable(99) ; 0 - 99
; Variable Index
Global NewMap *VariableIndex.udtVariable()
; Fill Dummy Data
For i = 0 To 50
Variable(i)\Name = "Var" + i
Variable(i)\Value = "Value " + Str(Random(1000))
Next
Procedure UpdateVariableIndex()
Protected i, cnt
ClearMap(*VariableIndex())
cnt = ArraySize(Variable()) - 1
For i = 0 To cnt
If Variable(i)\Name
*VariableIndex(Variable(i)\Name) = @Variable(i)
EndIf
Next
EndProcedure
UpdateVariableIndex()
Find.s = "Var40"
If FindMapElement(*VariableIndex(), Find)
Debug Find + " = " + *VariableIndex()\Value
Else
Debug Find + " not exists"
EndIf
Find.s = "Var51"
If FindMapElement(*VariableIndex(), Find)
Debug Find + " = " + *VariableIndex()\Value
Else
Debug Find + " not exists"
EndIf
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
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Is it possible to use array cell contents as variable names
You understand my intention more closely, but i oversimplified the problem. Instead of passing by value, or passing by reference, i am trying to create a complex array in which the contents might be passed by expression. Let me explain.mk-soft wrote: Wed Dec 24, 2025 11:35 am Instead of creating hundreds of variables, you can also work with structures and index.
Here is an example with an array and a map as an index.
...
The RPG I'm inventing will, because of the necessity of hundreds of calculations per frame, rely on a multidimensional array-type structure in which the contents may be constants, literals, names of variables to retrieve, or expressions that must be evaluated. Here's the kicker: the contents of any given cell might be changed to be any one of those criteria. A cell may be modified somewhere else in the program to be a constant, a stat name, or an expression using them.
It's like a function that's overloaded with dozens if not hundreds of possible parameter lists. But because it takes a long time (relatively speaking) for the processor to determine at runtime which of these functions applies, the need for speed requires that we break the function down to simple expressions. It would be like BASIC (or C++) in Assembly Language format — instruction, parameter. Simple and quick.
Structs can't be used because of their static memory allocation (based on type) and relatively bulky memory requirements — unless you can trim down their memory requirements dynamically on an as-needed basis. Arrays can't be used for the same reason, they would have to be reDIM'e every frame.
So i invented (at least conceptually) a Variable Matrix, an array that's handled by doing so many calculations per millisecond, then input checking, frame drawing, and time updating, and continuing its roster of calculations from where it left off. And all calculations must be done, because they rely on others.
The problem lies in allocating memory, HUNDREDS of possible reallocations per second, because of changes that happened last second. What would be the changes? Here is a short list:
- Your character moved to its next animation frame; the model vertices needs to be updated.
- The model has changed position in space: the lighting has changed and therefore the shadows and shading must be updated and redrawn.
- The textures might not be baked, and may not be static; the textures need to be recalc'd and drawn according to its new frame and the camera position.
- Reflective surfaces rely on camera position and visual impedance (like fog, distance, etc). This needs to be recalc'd and drawn.
- All of these things so far (model position, lighting changes, textural changes) may be affected by influences from multiple external sources {a dynamic aura from another character, object collision or even proximity, world discovery of something previously unknown or installed, etc).
- The sound space has changed; the reverb will need to be recalc'd
- A character's stats changed because of his afflicted conditions that also change temporally. Because that stat changed, another is affected, and so on...
- Effects of radial auras or even light fields on external factors dependent on distance from objects or other characters has shifted in proximity or modified because of time or other influences, so all of these need to be recalc'd.
- All of these things need to be multiplied by the number of player characters, because they have their own matrices of dynamic stats which may be influenced by multiple external or internal sources. I would like to allow a 20-member party, if the player wishes.
- The world will be running on its own. Creatures will have their own paths and routines, and they may be modified based on encounters with other NPCs. It's easy to update their position in their routine, but the graphics for these dynamic changes will need to be drawn if they are within camera view.
- Called scripts will need to be run either line-by-line per frame or all at once if instructed.
The program needs a softcodable array (much like the primitive calc fields in Diablo II 1.09+), for efficient tweaking and modding. The rows and columns (XY) can be static and unchanging (we are not adding new variables), but the Z will not, as influences on these variables would be quite dynamic. These influences would need to be culled and compressed instead of simply zeroed out, because i anticipate, MILLIONS of zeroes (everything doesnt influence everything necessarily), and every unnecessary 0 is an unnecessary variable retrieval, which eats up time and takes up RAM. Which is fine for a small program; this, however, will be bordering on Reality.
What we're looking for here is the softcoded capabilities of D2LOD, the graphics capabilities of Unreal, the ease of use in Unity, and the low-level paradigm of Machine Language, without the restrictions of static memory allocation, class membership, or type designation of C++. I don't know if PB can do it if it can't be handled in C++. Hence this exploratory post.
So, if PB can achieve cell content parsing with Maps, as shown in @mk-soft's code, this may be doable. The problem is that the example uses conditionals to determine if this varable is in a cell. What i need to do is use whatever possible variable name is in a given cell to do a simple calculation. If the cell says "mileage", a function accessing that cell's contents will use the value of the variable mileage directly, without using if-then or select-case.
Re: Is it possible to use array cell contents as variable names
Maybe store an address then instead of a variable name. @mileageHarbinger wrote: Wed Dec 24, 2025 4:13 am Let's use a simple one cell array. I need to check the variable whose name is in the cell. For example, the string in the cell is "mileage" , which is also a variable name.
Re: Is it possible to use array cell contents as variable names
I do not understand exactly what you want to do. But it sounds like the solution is similar to RPN(reverse polish notation) calculation.
There you can use a Stack of Pointers to call the correct Procedures Add,Sub,Mul,Div ... and the constants or variables are Pointers too.
If you are not firm with the very deep details of PB it's very tricky to get it.
Here is a code where the Grid/Arry Entry is a little Struktre containig a Pointer and a second value with the Type of the Pointer.
With this you can handle values and Function Pointers like for your constants and functions 'mileage' ...
There you can use a Stack of Pointers to call the correct Procedures Add,Sub,Mul,Div ... and the constants or variables are Pointers too.
If you are not firm with the very deep details of PB it's very tricky to get it.
Here is a code where the Grid/Arry Entry is a little Struktre containig a Pointer and a second value with the Type of the Pointer.
With this you can handle values and Function Pointers like for your constants and functions 'mileage' ...
Code: Select all
EnableExplicit
Enumeration EGrindEntryType
#GE_pProc = 0 ; GridEntry Type is Procedure
#GE_pInt
#GE_pDouble
EndEnumeration
; Prototype for Pointer called Grid Procs
Prototype InvokeProc()
Structure pGrindAny ; AnyPointer Structure for the Grid Pointer Entry
StructureUnion
Int.Integer ; integer
Dbl.Double ; double
Proc.InvokeProc ; Procedure Pointer of Prototype InvokeProc()
EndStructureUnion
EndStructure
Structure TGridEntry ; The Grid Data Structure
*Any.pGrindAny
Type.i
EndStructure
; Pointer called Procs
Procedure proc_mileage()
Debug "Procedure mileage called"
EndProcedure
Procedure proc_whatever()
Debug "Procedure whatever called"
EndProcedure
NewMap *mapProcs() ; IndexMap for the Procedure Pointers
Global Dim Cint.i(10) ; Integer constants
Global Dim Cdbl.d(10) ; Double constants
; Add Pointer of proc_mileage to IndexMap
Debug "Procedure Pointer mileage"
*mapProcs("mileage") = @proc_mileage()
Debug @proc_mileage()
Debug *mapProcs()
; Add Pointer of proc_whatever to IndexMap
Debug "Procedure Pointer whatever"
*mapProcs("whatever") = @proc_whatever()
Debug @proc_whatever()
Debug *mapProcs()
Debug ""
Global Dim Grid.TGridEntry(10)
; **** INIT ****
Define I, *ret
; Set values for our constants
For I = 0 To 10
Cint(I) = I ; integer Constants
Cdbl(I) = I/10 ; Double Constants
Next
; Grid(0) = Pointer to proc_mileage : search ProcPointer of mileage in IndexMap
With Grid(0)
\Type = #GE_pProc
; Attention FindMapElement() deliver the Pointer to the Data, not the Data
\Any =FindMapElement(*mapProcs(), "mileage")
Debug \Any ; Now Any points to the Memory where the ProcPointer of 'mileage' is stored in the Map
EndWith
; Grid(0) = Pointer to proc_whatever : search ProcPointer of whatever in IndexMap
With Grid(1)
\Type = #GE_pProc
\Any=FindMapElement(*mapProcs(),"whatever")
Debug \Any
EndWith
; Grid(2..5) -> Integer Constants
For I = 2 To 5
With Grid(I)
\Type = #GE_pInt
\Any = @Cint(I)
EndWith
Next
; Grid(6..10) -> Double Constants
For I = 6 To 10
With Grid(I)
\Type = #GE_pDouble
\Any = @Cdbl(I)
EndWith
Next
; ***** process the Grid *****
Debug ""
Debug "process Grid"
Debug ""
For I = 0 To 10
Select Grid(I)\Type
Case #GE_pProc
With Grid(I)
If \Any ; if Pointer is Not 0
\Any\Proc() ; Call the Proc by Pointer over the Prototype InvokeProc
Else
Debug "ProcPointer in Grid = 0"
EndIf
EndWith
Case #GE_pInt
Debug Str(I) + " : IntValue = " + Str(Grid(I)\Any\Int\i)
Case #GE_pDouble
Debug Str(I) + " : FloatValue = " + StrD(Grid(I)\Any\Dbl\d)
EndSelect
Next
Re: Is it possible to use array cell contents as variable names
Thanks for trying to implement my vision in PB, @SMaag. Im having trouble understanding all of it because both pointers and PB's Maps are not instinctive to me. You commented some, but not enough.
But your comment about my idea resembling RPN compelled me to look that up on Wikipedia, and that's exactly what i envisioned my cell contents would have to be — a single operation with operand and operator. The first operand will be the working value, the second operand will be the literal or variable that affects it, and the operator determines how the operand will affect the working value.
Imagine a 100 x 100 array (we'll call it the "ground level") stacked under a third dimension of cells who knows how many layers deep, except that all the cells in the ground level have different Z number of cells. Like 100x100 square city blocks, each block with one building but with different numbers of floors! Not only that, the number of floors on any one "building" may be modified at runtime. Already static memory allocation is out the window!
Now imagine that any floor in any building may represent a literal (unchanging value), a variable (whose stored value might have changed), a reference to another floor in the same or another building (which very likely changed since last calculation cycle), OR a single, simple operation of any of these. Based on other criteria in the program, any of these cells may be given new contents of any of the above types, so we never know what's in that field until the next round of calculations.
Now, the program engine's PRIMARY function is not to get user input (a secondary function that will be allowed to "interrupt" the calc cycle), but to iterate thru all the calculations that need to be made, starting at the top floor of each cell from the "block" at 0,0 all the way to 99,99. Each calculation evaluates that cell's operation or retrieval, and holds it in a temporary place, say the second floor of the building, and we'll call that the "working value." The rest of that level's calculations will continue for all "buildings."
So, it's like, instead of one Function (or Procedure in PB) evaluating a number of expressions before returning a result, we're doing the equivalent of evaluating one operation in the Function, hold that value, then leave that function, and do the same nth expression in another function. Remember, we don't want to use functions (procedures) because in any function, we dont know how many operations will need to be done, plus the expressions inside the function may need to be changed, and evaluating conditionals too can take a relatively large amount of time.
When any cell has its contents evaluated or retrieved, this will affect the working value and the result will replace the old working value for that "building" with the new result. Finally, when we've gone down thru each "level" of each "building", having made all influential calculations, the working value on the "second floor" replaces the final value (in that cell on the "ground floor") and that is the CURRENT value for that array cell.
Now all the other cities buildings can be done, And we have a limited time to do them! If our graphics are 30 frames per second, i got only 1/30 of a second to get hundreds if not thousands of calculations made. For example, particles have to be drawn and they may be influenced by many direct and indirect factors.
I hope that helps in the understanding of the VM. It's a workable concept but it is difficult to describe its efficacy in a language made of semidynamic memory allocation. C++ is rich with functionality, but it has its limitations based on current technology and routines.
But your comment about my idea resembling RPN compelled me to look that up on Wikipedia, and that's exactly what i envisioned my cell contents would have to be — a single operation with operand and operator. The first operand will be the working value, the second operand will be the literal or variable that affects it, and the operator determines how the operand will affect the working value.
Imagine a 100 x 100 array (we'll call it the "ground level") stacked under a third dimension of cells who knows how many layers deep, except that all the cells in the ground level have different Z number of cells. Like 100x100 square city blocks, each block with one building but with different numbers of floors! Not only that, the number of floors on any one "building" may be modified at runtime. Already static memory allocation is out the window!
Now imagine that any floor in any building may represent a literal (unchanging value), a variable (whose stored value might have changed), a reference to another floor in the same or another building (which very likely changed since last calculation cycle), OR a single, simple operation of any of these. Based on other criteria in the program, any of these cells may be given new contents of any of the above types, so we never know what's in that field until the next round of calculations.
Now, the program engine's PRIMARY function is not to get user input (a secondary function that will be allowed to "interrupt" the calc cycle), but to iterate thru all the calculations that need to be made, starting at the top floor of each cell from the "block" at 0,0 all the way to 99,99. Each calculation evaluates that cell's operation or retrieval, and holds it in a temporary place, say the second floor of the building, and we'll call that the "working value." The rest of that level's calculations will continue for all "buildings."
So, it's like, instead of one Function (or Procedure in PB) evaluating a number of expressions before returning a result, we're doing the equivalent of evaluating one operation in the Function, hold that value, then leave that function, and do the same nth expression in another function. Remember, we don't want to use functions (procedures) because in any function, we dont know how many operations will need to be done, plus the expressions inside the function may need to be changed, and evaluating conditionals too can take a relatively large amount of time.
When any cell has its contents evaluated or retrieved, this will affect the working value and the result will replace the old working value for that "building" with the new result. Finally, when we've gone down thru each "level" of each "building", having made all influential calculations, the working value on the "second floor" replaces the final value (in that cell on the "ground floor") and that is the CURRENT value for that array cell.
Now all the other cities buildings can be done, And we have a limited time to do them! If our graphics are 30 frames per second, i got only 1/30 of a second to get hundreds if not thousands of calculations made. For example, particles have to be drawn and they may be influenced by many direct and indirect factors.
I hope that helps in the understanding of the VM. It's a workable concept but it is difficult to describe its efficacy in a language made of semidynamic memory allocation. C++ is rich with functionality, but it has its limitations based on current technology and routines.
Re: Is it possible to use array cell contents as variable names
@Smaag
Thanks for the snippet, professional.
@Harbinger
Still hard to see the full plan. If this 100x100 array is a game map space, it's ok. If it's larger, maybe worth only updating the visible and close areas. Another not easy topic can be for this binary space partitioning, some kind of quadtree for this. Not really necessary, if the gamemap is small, and number of actors are few. It can be bruteforce computed then.
Thanks for the snippet, professional.
@Harbinger
Still hard to see the full plan. If this 100x100 array is a game map space, it's ok. If it's larger, maybe worth only updating the visible and close areas. Another not easy topic can be for this binary space partitioning, some kind of quadtree for this. Not really necessary, if the gamemap is small, and number of actors are few. It can be bruteforce computed then.
Re: Is it possible to use array cell contents as variable names
Code: Select all
@Harbinger
Still hard to see the full plan
@Harbinger
maybe we can split your Problem in a list of single Problems, what are easier to understand.
maybe you can explain the basic problem (not your solution of the problem), maybe with this we can find
a good Data Structrue solution for the problem. For me the questions is: is your solution the best way to solve your problem.



