02-15-2023, 03:53 AM
I was going to propose using _MEM but...
I was able to note the following:
That can't be done; at declaration BASIC needs to know what is the extent of the array. That is until something like "variants" and object-like variables is figured out into this implementation.
To try to respond to that, I devised this:
It's a bit confusing to follow along. Using subprograms makes code easier to read, to make things look as much as possible like multiple-dimension arrays.
Note the UDT variable "cards" is declared with "DIM SHARED" to make life easier into the subprograms. Also note the target type is always "INTEGER" in the _MEM workings, hence the "SHARED i0 AS INTEGER" appearing in a lot of places. The type has to be changed there as well as the last parameter to _MEMFILL, _MEMGET and _MEMPUT. Cannot be an UDT.
With this code, doing:
acard = obtain_card(1, 3)
would be like trying to do:
acard = test(1).abc(3)
in code (according to above) that actually is not possible in QB64(PE).
This code could benefit from some range-checking and other things to make it robust.
I was able to note the following:
Code: (Select All)
DIM AS INTEGER abc(1 TO 4)
DIM AS Test xyz(1 TO 10, abc())
That can't be done; at declaration BASIC needs to know what is the extent of the array. That is until something like "variants" and object-like variables is figured out into this implementation.
To try to respond to that, I devised this:
Code: (Select All)
TYPE cardty
nam AS STRING * 16
quant AS INTEGER
e AS _MEM
END TYPE
DIM SHARED card(1 TO 10) AS cardty
RANDOMIZE 1000
card(1).nam = "spade"
card(1).quant = 3
create_card 1
random_card 1
PRINT "The first card for "; card(1).nam; " is "; obtain_card(1, 1)
PRINT "The last card for "; card(1).nam; " is "; obtain_card(1, 3)
set_card 1, 3, 22
PRINT "The last card for "; card(1).nam; " was changed to "; obtain_card(1, 3)
delete_card 1
END
SUB create_card (which AS INTEGER)
SHARED i0 AS INTEGER
card(which).e = _MEMNEW(card(which).quant * LEN(i0))
_MEMFILL card(which).e, card(which).e.OFFSET, card(which).quant, 0 AS INTEGER
END SUB
SUB random_card (which AS INTEGER)
SHARED i AS INTEGER, i0 AS INTEGER
FOR i = 1 TO card(which).quant
_MEMPUT card(which).e, card(which).e.OFFSET + (i - 1) * LEN(i0), INT(RND * 52 + 1) AS INTEGER
NEXT
END SUB
SUB delete_card (which AS INTEGER)
_MEMFREE card(which).e
END SUB
SUB set_card (which AS INTEGER, subscr AS INTEGER, valu AS INTEGER)
SHARED i0 AS INTEGER
_MEMPUT card(which).e, card(which).e.OFFSET + (subscr - 1) * LEN(i0), valu AS INTEGER
END SUB
FUNCTION obtain_card% (which AS INTEGER, subscr AS INTEGER)
SHARED i0 AS INTEGER
obtain_card = _MEMGET(card(which).e, card(which).e.OFFSET + (subscr - 1) * LEN(i0), INTEGER)
END FUNCTION
It's a bit confusing to follow along. Using subprograms makes code easier to read, to make things look as much as possible like multiple-dimension arrays.
Note the UDT variable "cards" is declared with "DIM SHARED" to make life easier into the subprograms. Also note the target type is always "INTEGER" in the _MEM workings, hence the "SHARED i0 AS INTEGER" appearing in a lot of places. The type has to be changed there as well as the last parameter to _MEMFILL, _MEMGET and _MEMPUT. Cannot be an UDT.
With this code, doing:
acard = obtain_card(1, 3)
would be like trying to do:
acard = test(1).abc(3)
in code (according to above) that actually is not possible in QB64(PE).
This code could benefit from some range-checking and other things to make it robust.