Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Array in an array
#21
@QB64 community, @QB64 developers, @NasaCow

about this QB64 code
Code: (Select All)
OPTION _EXPLICIT

TYPE Test
    X AS INTEGER
    y AS INTEGER
    z AS STRING
END TYPE

TYPE UID
    ID AS INTEGER
END TYPE

DIM AS INTEGER abc(1 TO 4)
DIM AS Test xyz(1 TO 10, abc()) '<----- ??? is it possible to use a variable into declaration of an array???
' this time it is not a simple variable but it is an array just declared

abc(3) = 2
xyz(1, abc(1)).X = 5  ' this is xyz(1,0).X = 5  because abc(1) has value 0 for initialization
xyz(1, abc(2)).y = 3

10 PRINT xyz(1, abc(1)).X  ' this gives 5 a result because the xyz(1,0).X has been set to 5,  abc(1) = 0
PRINT xyz(1, abc(2)).y
PRINT abc(3)
20 PRINT xyz(1, abc(4)).X ' this gives 5 as result because the xyz(1,0).X has been set to 5, abc(4) = 0

after copying and pasting this code into QB64 I have got an "OK" while I was expecting an Error Alert!
Why does QB64 compiler accept the declaration of array with variable as index delimiter?

On running it is clear why line 10 and line 20 give the same result as output.
Waiting your opinions...
Reply
#22
another snippet for studying how QB64 manages the declaration of an array

Code: (Select All)
Option _Explicit
_Title "Array with variable as index"
Type Test
    X As Integer
    y As Integer
    z As String
End Type


Dim As Integer abc(1 To 4), m
m = 15 ' REMming this line of code and running this code you get
'        runtime error for subscript out of range
Dim As Test xyz(1 To 10, m)


m = 1
xyz(1, m).X = 5
m = 2
xyz(1, m).y = 3
m = 12
xyz(1, m).X = m
Rem m = 16
Rem xyz(1, m).X = m

m = 1
Print xyz(1, m).X
m = 2
Print xyz(1, m).y
m = 12
Print xyz(1, m).X
Rem m = 16
Rem Print xyz(1, m).X
Reply
#23
Quote:after copying and pasting this code into QB64 I have got an "OK" while I was expecting an Error Alert!

Why does QB64 compiler accept the declaration of array with variable as index delimiter?


The reason probably lies in this: A redimensioning takes place because the declaration of the array is not static. QBasic Manual Page 181:

"Dynamic arrays are always created if no constant value is given in the DIM statement - arrays declared as COMMON are also declared dynamically."

Static Arrays:
Const xyz = 100
Dim x(elements)
Dim p(10)
a! (2) = 3.1315 -> Array is implicitly declared static
Reply
#24
@nasacow

I have loosen the structure of data that you have planned.... I see a StudentType, a MasterAssignment and a SlaveAssignment.
In the first and in the last structure you use an ID to identify and  correlate the data. In MasterAssignment there is no ID.
You like to have all the data into one file.

I think that to keep in one file all the data you need to use an ID also for the third structure of data.
In this manner you can access to the searched data using the ID as index:
here an example

this is the file
________________________________________________________________________________________________
|long digit| records of StudentType|long digit|records of MasterAssignment|long digit|records of SlaveAssignment|CRC|
|_______ |___________________________ |_______________________ |______________________________|___ |
^              ^                                ^               ^                                    ^               ^                                   ^
 |               |                                 |                |                                     |                |                                    |
number of   |_________DATA         number of   |_________DATA             number of    |_________DATA            |__Security data
Student records                         Master Assignment                               Slave Assignment
                                                      records                                          records

while in RAM you load the data into 3 array with which you can work.
Reply
#25
Hi
@KernelPanic
I  do not know how and why I missed my 2 different answers to your post!

I said thank you to show me why is possible to use a variable in declaration of an array, doing this array Dynamic
so resuming:
Code: (Select All)
Dim Min As Integer
Min = 4
Dim MyStaticArray(1 To 4) As Integer '<---STATIC array
Dim MyDynamicArray(1 To Min) As Integer '<---Dynamic array
Rem Thank to Kernelpanic for showing this to me.
Reply
#26
Nope! It is not about using variables for lbound or ubound.

REDIM MyDynamicArray(... )  ' this is dynamic

DIM MyStaticArray(... ) ' this is static

Quiz: How do you reset all values to 0 (or "") in Static Array, how do you do it with Dynamic Array?
b = b + ...
Reply
#27
Quote:@KernelPanic

I  do not know how and why I missed my 2 different answers to your post!

Forget it! What do you think I have all missed in my life. . . Take it easy!  Tongue

Reply
#28
(02-21-2023, 01:07 AM)bplus Wrote: Nope! It is not about using variables for lbound or ubound.

REDIM MyDynamicArray(... )  ' this is dynamic

DIM MyStaticArray(... ) ' this is static

Quiz: How do you reset all values to 0 (or "") in Static Array, how do you do it with Dynamic Array?

If it's static "ERASE" is enough to reset all members.

Cannot do it to a dynamic array unless the programmer is ready to re-allocate it. Otherwise for an array could use a _MEM variable, compute the size of the array extent and use _MEMFILL to set all elements to zero. Need a variable of the same basic type as the array only to set to zero because _MEMFILL requires a reference, cannot be a constant. Might be able to do the same thing with an array of fixed-length strings. Cannot use _MEM toward VLS's so it's harder.

There's always the slow way of using a "FOR... NEXT" loop to set to zero or to the empty string.

Cannot use "CLEAR" because it blanks out all other variables whether or not they are static.
Reply
#29
@mnrvovrfc

ERASE is correct for Static array.

The easy answer for Dynamic array is to just REDIM it again.
b = b + ...
Reply
#30
(02-21-2023, 01:07 AM)bplus Wrote: Nope! It is not about using variables for lbound or ubound.

REDIM MyDynamicArray(... )  ' this is dynamic

DIM MyStaticArray(... ) ' this is static

Quiz: How do you reset all values to 0 (or "") in Static Array, how do you do it with Dynamic Array?
Hi dear 
Please read #23
Or at this link
Dim & Redim

DIM myArray (1 to 4) AS INTEGER ' this is static
DIM my2ndArray (1 to M)  AS INTEGER' This is dynamic and M =0


IMHO
Dynamic Array
When you change dimensions of array it is restored to default value (0 or "")
Reply




Users browsing this thread: 10 Guest(s)