|
Forum Index : Microcontroller and PC projects : Assign integer array name dynamically
| Author | Message | ||||
| panky Guru Joined: 02/10/2012 Location: AustraliaPosts: 1116 |
I would like to have the ability to create an integer array dynamically under program control but can't figure out how to do so (if it's even possible). The code below is pseudo code but gives you an idea of what I am trying to achieve. name$ = "IntArrayName" arraysize% = 10 sub CreateArray name$,arraysize% dim name$(arraysize) ' ... blah, blah, blah end sub ... this fails, as would be expected, with a Type error, but the questions remains, how could I achieve this? Any ideas most welcome, panky ... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it! |
||||
| erbp Senior Member Joined: 03/05/2016 Location: AustraliaPosts: 195 |
You should use different names for the variables and the subroutine arguments. You then need to call the subroutine passing the variable names. I suspect this will still not work, but it has a better chance than the code you posted. Try the following: arrayname$ = "IntArrayName" arraysize% = 10 CreateArray(arrayname$, arraysize%) sub CreateArray aname$, asize% dim aname$(asize%) ' ... blah, blah, blah end sub Phil. |
||||
| panky Guru Joined: 02/10/2012 Location: AustraliaPosts: 1116 |
... thanks Phil, but I need to pass a string name to create an integer array of that same name. The issue seems to be that I can't figure out a way to pass a string variable (ie. a name to be used in creating an array) to a create an integer array using a dim statement. It appears that you can't use a variable in the dim statement as the name of the array being defined. panky ... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it! |
||||
| erbp Senior Member Joined: 03/05/2016 Location: AustraliaPosts: 195 |
Hi Panky, That is what I thought would be the case, which is why I said I did not think it would work. I'm am pretty sure you are correct in that you cannot use a variable to contain the name of the variable you are trying to DIM. Phil. |
||||
MicroBlocks![]() Guru Joined: 12/05/2012 Location: ThailandPosts: 2209 |
It can not be done. Variables are declared in your program and are then parsed. The other problem is, how are you going to read those values. Every other piece of code will have to work with that variablename. It will get pretty messy very very quickly. Another way of doing it is to create a 2 dimensional array and a small array that holds the name of the 'variable'. You can then lookup the name in that small array, get the index of that item and use that as the first dimension in the 2 dimensional array. Basically you will then have an array that holds an array of values. [code] DIM STRING Names(100) DIM INTEGER Values(100,100) [/code] You would have to dimension the array for what you actually are using otherwise it will use a lot of memory. So if your have a name you go through the Names array until you find it. The index of that entry is then used to get the value form Values(index,0) until Values(index,100) There is another way that needs a bit more 'bookkeeping' but is more efficient. Instead of the 2 dimensional arrya you create a 1 dimesnional one that can hold all the values you will need. The Names array will then be an index to the startposition in that 1 dimenional array. [code] DIM STRING Names(100) DIM INTEGER Positions(100) DIM INTEGER Values(10000) [/code] You do the same so first lookup the name, then use the index to get a "Start" from Positions(index) and you also need the 'End' from Positions(index+1) The values will then be available from Values(start) to Values(end). There are many ways to solve a problem, one not always better then the other. Maybe if you describe WHY you need to do it will give some more context and maybe some solutions. Microblocks. Build with logic. |
||||
| CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2171 |
You are trying to use the expression evaluater where a literal is required. You can't do this. When you DIM a variable, the statement expects a literal name for the variable and the expression evaluater ( e.g. A$+b$) isn't employed in that process. you can see this in action here: > > gg=89 > input aa ? gg > ? aa 0 > if things worked as you hope, aa would be 89 i.e. when I entered gg as my argument, the expression evaluater would have determined that gg was equal to 89 and then put that value in aa... it didn't as the input expects a literal. The numeric value is only ascertained up to the first non-numeric character ... try this again with 2+2... aa=2 (not 4 as might be expected) because no expression was evealuated. Interestingly, ZX81 Basic would do this. |
||||
| panky Guru Joined: 02/10/2012 Location: AustraliaPosts: 1116 |
Hi Jean, Thanks for the detailed response. What I am trying to do is create a set of utilities to create, push onto and pop off a user define LIFO stack. I wanted to use an integer array as the actual data repository and treat it as an n x n byte stack that is user definable for name and size. For example, call a subroutine called CreateStack and pass it a stack name (to used by other parts of the program), a stack element width in bytes and a stack depth in stack elements. The actual stack would be an integer array and I would keep track of where the next stack entry would go for push and pop operations. The actual data going onto and off the stack would be a string of set maximum length and would be post padded with nulls if less than maximum length. It is a simple matter to do this (and I have done it) with predefined stack width, depth and stack name but I would like to do it dynamically just as we do with a DIM statement. panky ... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it! |
||||
| CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2171 |
Interesting. I wonder if you could use PEEK/VAR, POKE & Co to access the variable table to determine names as passed in to your functions then simply move lumps of memory around. Maybe Geoff or Pete M could comment on whether the name table is available and any interesting structure that you could get hold of. If you were passed a string (that is a variable name) you'd have to manually search the variable table to find the pointers etc but then just moving lumps of data around is easy enough (if cryptic). |
||||
MicroBlocks![]() Guru Joined: 12/05/2012 Location: ThailandPosts: 2209 |
I still would not know of a way to use the values that are stored in a dynamically named array. if you have a piece of code how would you then change the code to read specifically from that dynamically created variable. It would only be possible if you also have dynamically created code. Microblocks. Build with logic. |
||||
| CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2171 |
... unless the array was used as an alias in some way under the control of the stacking code. So a finite number of variables permitted and a similar number of array elements. It could consume memory at a scary rate. I did a similar mapping process a few years back to permit dynamically generated SQL queries where I read the column names from the system tables and they were numbered 1,2,3,4... etc and that maps nicely onto elements in an array. That way I didn't need to know before hand what the column names were in the table, I just picked them from the system internals and used the respective array element to build SQL queries on the fly. Similar approach if not directly relevant. |
||||
| Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3308 |
The variable's name is stored in the first 32 bytes of each entry in the variable table. It is zero padded to 32 bytes and in upper case. Using this fact you could employ PEEK VARTABLE to find the start of the variable table in memory and step up through the table to search for a specific variable by name. Thus it would be possible to create an array with a temporary name, step through the table to find its entry then rewrite the name with the name of your choice using POKE. As MicroBlocks points out you would still not be able to access this variable using a name held in a variable but you could use PEEK VAR to access the memory of this variable and POKE VAR to change its value. Extremely messy and probably not worth the trouble. Geoff Graham - http://geoffg.net |
||||
| panky Guru Joined: 02/10/2012 Location: AustraliaPosts: 1116 |
Thanks Geoff, Microblocks and CaptainBoing - it was one of those late night "ideas" that sometimes happen and fall into the catagory of "it seemed like a good idea at the time" Oh well onwards and upwards for the next hairbrained idea Regards all, panky ... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it! |
||||
| Paul_L Guru Joined: 03/03/2016 Location: United StatesPosts: 769 |
DBMS languages, like VisualFoxPro and dBase, use what they refer to as macro expansion. You can store a filename in a named variable or array element like fnam$="thisfile.txt" then enclose the variable name in parenthesisOPEN (fnam$) FOR APPEND AS #1 or prefex the name with a "macro expansion" operatorOPEN &fnam$ FOR APPEND AS #1 .The dBase interpreter understands this as OPEN (whatever is contained inside fnam$) FOR APPEND AS #1 .It's sort of like storing a pointer to some other place in a named variable in C. BASIC never could do anything like this. However .... our resident genius Geoff has implemented the EVAL(t$) fuction which can evaluate anything contained in t$ as a BASIC expression. It might be possible to trick EVAL(t$) into returning a true file name. Geoff ....... could this work????? Paul in NY |
||||
| CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2171 |
eh? I must be missing something here. ![]() Every basic I have ever worked with can open files using a string expression (except MMBasic & ZX81 Basic coz it doesn't have innate file commands !=MMX) even "complex" stuff like OPEN MyDirectory$+"/"+MID$(STR$(mynumber),2)+".txt" works. I have used it thousands of times in dozens of varieties of Basic am I being thick? |
||||
| Paul_L Guru Joined: 03/03/2016 Location: United StatesPosts: 769 |
Hi Captain, no, you're not being thick but you're confusing defined variables or literals with unknown variables. In your code MyDirectory$ and mynumber point to a definite location in memory where you have previously stored some definite value. The DBMS expansion functions all enable the language to go find a value that is not in memory but is in some indeterminate place out in a data file. It might even have to find the location using an index. It usually requires some form of just in time compilation of program code which is not known at compilation time. This gets blurry because BASIC is mostly an interpreted language where each individual statement is "compiled" when it is read. In a true interpreter there is little difference between in line code and remote code which is pointed to inline somehow. In the case of dBase and FoxProWindows they are P code interpreters. They partially compile big chunks of code before they try to run it step by step, but they don't try to write out an object file and then link it to libraries. They hold the entire library in RAM and link to it at run time. The EVAL() function looks at the contents of its parameter at run time as if it is part of the code, not part of the data. Paul in NY |
||||
| JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4126 |
To get a feeling for what you're wanting - why? Explain please. --- In some languages you can do the equivalent of do input line a$ eval(a$) loop And then type in things like: dim b(5) b(3) = 1 print b(3) I think EVAL doesn't support those sorts of things. It's not what MMBasic (or most Basics) are designed to do. It does tend to mean that the language has to be interpreted (or JIT - Just In Time - compiled). MMBasic could be that but it would be a lot bigger and probably slower too. John |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |