Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:05 02 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 : CMM2: any chance of some 'love' for string arrays ?

Author Message
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 10:28am 07 Nov 2020
Copy link to clipboard 
Print this post

Hi folks (and I will pretend someone other than Peter can help),

Unless I'm much mistaken MATH SET and MATH SLICE only work with numerical arrays (the clue is in the name) and there are no MEMORY SET/COPY variants specifically for working with STRINGs.

In theory the raw MEMORY SET/COPY could be used but I think we are missing a way to get a crucial piece of information that would allow them to be used generally to manipulate string arrays, that is a way to retrieve the LENGTH used in the declaration of the string array. Can anything be done about this (or can someone tell me how to PEEK this information).

And rather more specialised is there any chance of SORT getting a couple of additional parameters so we can just sort part of an array ? I know it's a strange request but I have a reputation to keep up ;-) My particular use-case is using a string array to store a sorted map where I am storing both keys (in the first half) and values (in the second half) of a single array. I could of course use a 2D array, but then I can't use SORT at all.

Best wishes,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10315
Posted: 10:53am 07 Nov 2020
Copy link to clipboard 
Print this post

If you download V5.05.06RC14 again then you can try the following




Remember the string has a length byte at the beginning so you need to move 1 byte more than the reported length

I don't understand the sort request. Can you not do what you want by using the optional index and having two arrays?

struct s_vartbl {                               // structure of the variable table
char name[MAXVARLEN];                       // variable's name
   char type;                                  // its type (T_NUM, T_INT or T_STR)
   char level;                                 // its subroutine or function level (used to track local variables)
   unsigned char size;                         // the number of chars to allocate for each element in a string array
   char dummy;
   int __attribute__ ((aligned (4))) dims[MAXDIM];                     // the dimensions. it is an array if the first dimension is NOT zero
   union u_val{
       MMFLOAT f;                              // the value if it is a float
       long long int i;                        // the value if it is an integer
       MMFLOAT *fa;                            // pointer to the allocated memory if it is an array of floats
       long long int *ia;                      // pointer to the allocated memory if it is an array of integers
       char *s;                                // pointer to the allocated memory if it is a string
   }
}

Edited 2020-11-07 21:06 by matherp
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 11:41am 07 Nov 2020
Copy link to clipboard 
Print this post

Thanks Peter, PEEK(VARHEADER ...) should let me do what I want.

Forget about the SORT request.

For what it is worth I'm writing the code to support the following:

Dim my_map$(map.new%(20))
map.init(my_map$())

map.put(my_map$(), "foo", "bar")
map.put(my_map$(), "wom", "bat")
map.put(my_map$(), "aaa", "bbb")

Print map.size%(my_map$())
Print map.get$(my_map$())
Print map.get$(my_map$())
Print map.get$(my_map$())


Where 'map' is a sorted map implementation using half of the array for the keys, half for the values and the final element for the size.

This allows the caller to treat my_map$() as a single variable, not separate keys, values and size.

Tom
Edited 2020-11-07 21:47 by thwill
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
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