Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 10:36 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 : Doing a MATH SLICE by reference. Possible?

Author Message
danielkos
Newbie

Joined: 10/07/2021
Location: Australia
Posts: 25
Posted: 04:48am 15 Aug 2021
Copy link to clipboard 
Print this post

I've come across a few situations where I have a multi-dimensional array and I'd like to use just one of the slices (e.g. something like myArray(4,) to act on the 4th 'row' of the 2D array) in an expression or as an argument to a subroutine.

MATH SLICE and MATH INSERT will let me 'unzip' and 'rezip' my 2D array together by creating a copy of the array but I'm concerned about the speed of making a new copy of something where I only need to act on the original. Maybe this is just the C part of my brain talking here and missing pointers. Am I being too paranoid?
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 06:26am 15 Aug 2021
Copy link to clipboard 
Print this post

C has a lot to answer for...   >spit!<

First, don't expect C speeds from a BASIC interpreter. It's going to be slower and there's nothing you can do about it apart from, possibly, do the fast part of your program in CSubs.

Can you do anything with POKE VAR to modify a slice in situ?
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 07:42am 15 Aug 2021
Copy link to clipboard 
Print this post

  Quote   Am I being too paranoid?

Yes: slice and insert are pretty efficient and compared to other Basic overhead will not significantly affect performance.
 
danielkos
Newbie

Joined: 10/07/2021
Location: Australia
Posts: 25
Posted: 12:01pm 15 Aug 2021
Copy link to clipboard 
Print this post

Thanks Mixtel90 and matherp. That's fair enough, I think MATH SLICE and MATH INSERT will do for what I need after all.

  Quote  C has a lot to answer for...   >spit!<

I was a bit incredulous at first when I found there weren't any structs in MMBASIC but now I'm quite enjoying the coding experience, can get the same stuff done, I just needed to break out of some dogmatic thinking.

  Quote  Can you do anything with POKE VAR to modify a slice in situ?

I actually spent some time going through the manual looking for a way to do something rather crazy compared to that. I was looking for an analogue to the CALL command to allow a variable name to be constructed from a string expression. Then I could avoid slicing altogether by using lots of 1D arrays and ...put the second dimension index into the name of the variable itself, but with the disadvantage of not being able to forgive myself when re-reading the same code next week. I... um... decided against it.
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 442
Posted: 06:28pm 15 Aug 2021
Copy link to clipboard 
Print this post

  danielkos said  Thanks Mixtel90 and matherp. That's fair enough, I think MATH SLICE and MATH INSERT will do for what I need after all.

  Quote  C has a lot to answer for...   >spit!<

I was a bit incredulous at first when I found there weren't any structs in MMBASIC but now I'm quite enjoying the coding experience, can get the same stuff done, I just needed to break out of some dogmatic thinking.

  Quote  Can you do anything with POKE VAR to modify a slice in situ?

I actually spent some time going through the manual looking for a way to do something rather crazy compared to that. I was looking for an analogue to the CALL command to allow a variable name to be constructed from a string expression. Then I could avoid slicing altogether by using lots of 1D arrays and ...put the second dimension index into the name of the variable itself, but with the disadvantage of not being able to forgive myself when re-reading the same code next week. I... um... decided against it.


Do you have any examples of code to replace structures? Right now, I use array elements to simulate them:

Instead of:
STRUC MyRecord
 Name as STRING
 DOB as STRING
 Addr_Line_1 AS STRING
 Addr_Line_2 AS STRING
END STRUC

DIM StudentFile(1000) AS MyRecord

StudentFile(10).Name = "Stein, Frank N."
StudentFile(10).DOB = "31/10/04"
StudentFile(10).Addr_Line_1 = "Castle Frankenstein"
StudentFile(10).Addr_Line_2 = "Darmstadt"

I write

Name = 1
DOB = 2
Addr_Line_1 = 3
Addr_Line_2 = 4

DIM StudentFile$(1000,4)

StudentFile$(10,Name) = "Stein, Frank N."
StudentFile$(10,DOB) = "31/10/04"
StudentFile$(10,Addr_Line_1) = "Castle Frankenstein"
StudentFile$(10,Addr_Line_2) = "Darmstadt"

Edited 2021-08-16 04:31 by toml_12953
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 09:16pm 15 Aug 2021
Copy link to clipboard 
Print this post

  toml_12953 said  Do you have any examples of code to replace structures? Right now, I use array elements to simulate them:


That approach is memory intensive especially with the default 256 byte strings, i.e.
DIM StudentFile$(1000,4) is allocating 1000 kB of memory.

1. You could reduce this by specifying a LENGTH < 255 in the string array declaration - note that a LENGTH specification gains you nothing for a scalar string variable even though the interpreter won't complain.

2. If the fields you need to store in "structures" are small then you can store the structure as a delimiter separated string and use the FIELD$() function to extract individual fields ... though setting requires more initiative.

3. For full flexibility declare integer arrays and use them as blocks of memory which you manipulate with PEEK, POKE, MEMORY SET and MEMORY COPY. You will probably want to write Function/Sub wrappers around the manipulation to keep things sane but you risk running up against the 512 FUNCTION/SUB limit. I occasionally think about writing a tool to generate such code from a STRUCTURE specification but haven't found the time/need to so yet.

I don't really have any code to share that exactly meets your requirements, you might (if brave / foolhardy) look at https://github.com/thwill1000/cmm2-sptools/blob/master/src/splib/txtwm.inc which uses the memory block approach to store "window" structures. However because I'm only ever interested in the current window it doesn't provide simple field accessors for specific windows instead it provides the twm.switch() method that:
   - stores the fields of the old current window (stored in global variables) in the memory block
   - copies the fields of the new current window from the memory block into the global variables.

Best wishes,

Tom
Edited 2021-08-16 07:17 by thwill
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 09:23pm 15 Aug 2021
Copy link to clipboard 
Print this post

One other thing you can do is use "." inside variable names and if you squint real hard fool yourself you have an approximation of structures or namespaces, thus:

CONST MAX_NUM_STUDENTS = 1000
DIM student.name$(MAX_NUM_STUDENTS) Length 64
DIM student.dob$(MAX_NUM_STUDENTS) Length 8
DIM student.addr_line_1$(MAX_NUM_STUDENTS) Length 64
DIM student.addr_line_2$(MAX_NUM_STUDENTS) Length 64
DIM student.some_integer_field%(MAX_NUM_STUDENTS)
DIM student.some_real_field!(MAX_NUM_STUDENTS)


Best wishes,

Tom
Edited 2021-08-16 07:24 by thwill
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 442
Posted: 12:09am 16 Aug 2021
Copy link to clipboard 
Print this post

  thwill said  One other thing you can do is use "." inside variable names and if you squint real hard fool yourself you have an approximation of structures or namespaces, thus:

CONST MAX_NUM_STUDENTS = 1000
DIM student.name$(MAX_NUM_STUDENTS) Length 64
DIM student.dob$(MAX_NUM_STUDENTS) Length 8
DIM student.addr_line_1$(MAX_NUM_STUDENTS) Length 64
DIM student.addr_line_2$(MAX_NUM_STUDENTS) Length 64
DIM student.some_integer_field%(MAX_NUM_STUDENTS)
DIM student.some_real_field!(MAX_NUM_STUDENTS)


Best wishes,

Tom


I'll probably go with the FIELD$ function. I wish we could use FIELD as we did in the 70's and '80s, though!

FIELD 1, 64 AS Name$, 8 AS DOB$, 64 AS Addr_Line1$, 64 AS Addr_Line2$


Then use LSET and RSET to fill the fields.
 
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