Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 09:13 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MMBasic question, array assignment

Author Message
abraxas
Regular Member

Joined: 16/06/2020
Location: Canada
Posts: 99
Posted: 10:20pm 15 Jul 2020
Copy link to clipboard 
Print this post

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: Germany
Posts: 1593
Posted: 10:32pm 15 Jul 2020
Copy link to clipboard 
Print this post

  abraxas said  Is it possible to assign an array to another variable so you can have a level of indirection?


No.

You have to copy each array element individually.

Michael
Edited 2020-07-16 08:36 by twofingers
causality ≠ correlation ≠ coincidence
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3378
Posted: 10:36pm 15 Jul 2020
Copy link to clipboard 
Print this post

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 fruitoftheshed
 
abraxas
Regular Member

Joined: 16/06/2020
Location: Canada
Posts: 99
Posted: 10:37pm 15 Jul 2020
Copy link to clipboard 
Print this post

  twofingers said  
  abraxas said  Is it possible to assign an array to another variable so you can have a level of indirection?


No


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: Canada
Posts: 99
Posted: 10:39pm 15 Jul 2020
Copy link to clipboard 
Print this post

  lizby said  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.


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: Canada
Posts: 1132
Posted: 11:02pm 15 Jul 2020
Copy link to clipboard 
Print this post

  abraxas said  I want to have a reference that refers to a different array at different times.

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: Canada
Posts: 99
Posted: 11:35pm 15 Jul 2020
Copy link to clipboard 
Print this post

  vegipete said  
  abraxas said  I want to have a reference that refers to a different array at different times.

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: Australia
Posts: 1114
Posted: 11:37pm 15 Jul 2020
Copy link to clipboard 
Print this post

@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: Canada
Posts: 99
Posted: 12:54am 16 Jul 2020
Copy link to clipboard 
Print this post

  panky said  @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


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 States
Posts: 3378
Posted: 12:15pm 16 Jul 2020
Copy link to clipboard 
Print this post

  abraxas said  Using EVAL for this seems like an expensive way to dereference the elements...

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 fruitoftheshed
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 01:52pm 16 Jul 2020
Copy link to clipboard 
Print this post

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
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025