|
Forum Index : Microcontroller and PC projects : MMBasic question, array assignment
| Author | Message | ||||
| abraxas Regular Member Joined: 16/06/2020 Location: CanadaPosts: 99 |
Is it possible to assign an array to another variable so you can have a level of indirection? For example I’m trying to do a swap of arrays but it doesn’t work or I’m getting the syntax wrong: DIM foo(10) AS INTEGER DIM bar(10) AS INTEGER DIM baz() = foo() Above won’t work no matter what combinations of brackets or no brackets I’m trying to use. Likewise I haven’t been able to return an array from a function. I tried to do that as a substitute for swapping them via references. |
||||
| twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1769 |
No. You have to copy each array element individually. Michael Edited 2020-07-16 08:36 by twofingers ‚My name is Ozymandias, king of kings Look on my works, ye Mighty, and despair!‘ Nothing beside remains. Round the decay Of that colossal wreck, boundless and bare The lone and ... |
||||
| lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3881 |
Can you explain further what you mean by "a level of indirection"? I assume you don't mean: for i = 1 to 10: bar(i)=foo(i): next i Or: for i = 1 to 10: j=bar(i): bar(i)=foo(i): foo(i)=j: next i While you can't return an array in a function, you can manipulate global arrays in a sub, and I believe can manipulate arrays passed (by reference) to a sub. PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on FOTS |
||||
| abraxas Regular Member Joined: 16/06/2020 Location: CanadaPosts: 99 |
Well, that was succinct... how about the second part of my question, the part about returning arrays from functions? |
||||
| abraxas Regular Member Joined: 16/06/2020 Location: CanadaPosts: 99 |
I assume you don't mean: for i = 1 to 10: bar(i)=foo(i): next i Or: for i = 1 to 10: j=bar(i): bar(i)=foo(i): foo(i)=j: next i While you can't return an array in a function, you can manipulate global arrays in a sub, and I believe can manipulate arrays passed (by reference) to a sub. No, it’s not what I’m after. I want to have a reference that refers to a different array at different times. |
||||
| vegipete Guru Joined: 29/01/2013 Location: CanadaPosts: 1185 |
Perhaps you can use an array of arrays. Then your reference is just the index of the desired array. dim foobar(2,100) put foo(N) into foobar(1,N) put bar(N) into foobar(2,N) Use 1 to access foo, 2 to access bar. Visit Vegipete's *Mite Library for cool programs. |
||||
| abraxas Regular Member Joined: 16/06/2020 Location: CanadaPosts: 99 |
Perhaps you can use an array of arrays. Then your reference is just the index of the desired array. dim foobar(2,100) put foo(N) into foobar(1,N) put bar(N) into foobar(2,N) Use 1 to access foo, 2 to access bar. Great idea, that’s probably how I’m going to solve it. |
||||
| panky Guru Joined: 02/10/2012 Location: AustraliaPosts: 1134 |
@abraxas, Not sure if this will help but the EVAL command may be a way to achieve what you want? a=0 ' array index c="x$" ' array name e$=str$(a) ' array index as string dim x$(2) ' 3 element string array x$(0)="hi" ' array first slot x$(1)="lo" ' array second slot ' do something with the array print eval(c$+"("+e$+")") ' use EVAL to plug in variable names So, by changing just a and c$ you could reference any element of any array. (tested on a CMM2 panky ... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it! |
||||
| abraxas Regular Member Joined: 16/06/2020 Location: CanadaPosts: 99 |
Not sure if this will help but the EVAL command may be a way to achieve what you want? a=0 ' array index c="x$" ' array name e$=str$(a) ' array index as string dim x$(2) ' 3 element string array x$(0)="hi" ' array first slot x$(1)="lo" ' array second slot ' do something with the array print eval(c$+"("+e$+")") ' use EVAL to plug in variable names So, by changing just a and c$ you could reference any element of any array. (tested on a CMM2 panky Using EVAL for this seems like an expensive way to dereference the elements... It's surprising that MMBasic doesn't let us do what I tried to do in the first place. Since every variable is internally passed by reference it should be quite natural to add as a language feature. |
||||
| lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3881 |
Still not sure exactly what you're looking for--something like a pointer in C, which could point to an element in one array, or to an element in another? PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on FOTS |
||||
| Tinine Guru Joined: 30/03/2016 Location: United KingdomPosts: 1646 |
Wish I had the time to create a library of such things. No need to bloat MMBasic with functions that are rarely used. /* copying one array to another */ for(i = 0; i < Size; i++) { b[i] = a[i]; } Regards, Craig |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2026 |