![]() |
Forum Index : Microcontroller and PC projects : Doing a MATH SLICE by reference. Possible?
Author | Message | ||||
danielkos Newbie ![]() Joined: 10/07/2021 Location: AustraliaPosts: 25 |
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 KingdomPosts: 7937 |
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 KingdomPosts: 10310 |
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: AustraliaPosts: 25 |
Thanks Mixtel90 and matherp. That's fair enough, I think MATH SLICE and MATH INSERT will do for what I need after all. 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. 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 StatesPosts: 442 |
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 KingdomPosts: 4311 |
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 KingdomPosts: 4311 |
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 StatesPosts: 442 |
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. |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |