114 lines
4 KiB
Text
114 lines
4 KiB
Text
'Task
|
|
'Show basic array syntax in your language.
|
|
|
|
'Basically, create an array, assign a value to sit, and retrieve an element (if available, show both fixed-length arrays and dynamic arrays, pushing a value into it).
|
|
Rem DECLARATION PART
|
|
|
|
Rem QB64/QuickBasic/Qbasic array examples
|
|
'-----------
|
|
MyArray%(10) = 11 ' it creates an array integer of 11 elements from 0 to 11
|
|
Dim MyArray2%(0 To 10) ' it is equal to the previous line of code, it is its explicit way
|
|
|
|
'--------------
|
|
Option Base 1 ' from here all arrays have as default as index of first item 1
|
|
MyArray3%(10) = 14 'it creates array from 1 to 14
|
|
|
|
Dim Myarray4%(1 To 14) ' it is equal to the 2 previous lines of code
|
|
|
|
'-----------------
|
|
Dim Shared myArray$(-12 To 12) ' it creates a string array with 25 elements and SHARED makes it global array
|
|
|
|
'---------------------
|
|
'$dynamic
|
|
Dim MyArray1!(1 To 4) ' these two lines of code create a resizable array
|
|
|
|
ReDim myArray2!(1 To 4) ' it does the same of the 2 previous lines of code
|
|
|
|
'-------------------------
|
|
' alternatively at the place of suffix ! or $ or % or # or &
|
|
' you can use the explicit way "DIM namearray (start to end) AS TypeOfVariable"
|
|
Dim MyIntegerArray(1 To 10) As Integer
|
|
Dim MyStringArray(1 To 10) As String
|
|
Dim MySingleArray(1 To 10) As Single
|
|
Dim MyLongArray(1 To 10) As Long
|
|
Dim MyDoubleArray(1 To 10) As Double
|
|
|
|
'---------------------------
|
|
'it is possible defining an User Data Type and creating an array with that type
|
|
Type MyType
|
|
Name As String * 8 ' a fixed lenght string variable
|
|
ID As Integer
|
|
Position As Single
|
|
End Type
|
|
|
|
Dim Lists(1 To 10) As MyType
|
|
ReDim WhoIs(-19 To 10) As MyType
|
|
|
|
|
|
'------------------------------
|
|
' Number of dimensions of an array: QuickBasic vs QB64
|
|
' an array can have from 1 to until 60 dimensions in QuickBasic
|
|
' an array can have from 1 to RAM dimension of your machine
|
|
' you must think that in the dayly practice is rare to use more than 3 dimensions
|
|
Dim Calendar%(1 To 31, 1 To 56, 1 To 12) ' array calendar with days, week, mounths
|
|
ReDim Time(1 To 60, 1 To 60, 1 To 24) As Integer ' array Time with seconds, minutes and hours
|
|
|
|
Rem ONLY QB64 arrays
|
|
'--------
|
|
' QB64 introduces more TypeOfVariable all of them associated to a suffix
|
|
' so you can declare also these kind of data
|
|
' _BIT or `, _BYTE or %%, _INTEGER64 or &&, _FLOAT or ##, OFFSET or %&, _MEM (no suffix)
|
|
Dim MyByteArray%%(1 To 4)
|
|
Dim MyByteArray2(1 To 4) As _Byte
|
|
' are the same declaration of the array
|
|
|
|
'----------------
|
|
'QB64 lets you to use an alternative way to declare variables and array
|
|
'using the following syntax: DIM / REDIM AS Type of data Array1, Array2, Array3"
|
|
ReDim As _MEM Mem1(1 To 5), Mem2(6 To 10)
|
|
Dim As _Unsigned _Byte UByte(-3 To 25), Ubyte2(100 To 200)
|
|
|
|
|
|
Rem QB64 / QB PDS (7.1) arrays
|
|
ReDim MyPreservedArray(1 To 5) As Integer ' it creates a dynamic array
|
|
ReDim _Preserve MyPreservedArray(-3 To 100) As Integer ' it changes limit of dimension of array
|
|
|
|
Rem ASSIGNING PART
|
|
' at declaration point each array is initializated: 0 for digit arrays, "" for string arrays
|
|
' in the UDT arrays each variable is initializated following its type
|
|
|
|
|
|
' all types of array can be accessed using the index of item choice to change
|
|
' in the UDT array each item of UDT is reached using the "." while the item of array needs the index
|
|
Print MyPreservedArray(2)
|
|
MyPreservedArray(2) = 12345
|
|
Print MyPreservedArray(2)
|
|
|
|
Print WhoIs(-10).Name
|
|
WhoIs(-10).Name = "QB64"
|
|
Print WhoIs(-10).Name
|
|
WhoIs(10).Name = WhoIs(-10).Name
|
|
Print WhoIs(10).Name
|
|
|
|
Rem RETRIEVE AN ELEMENT
|
|
' basically the coder must use a loop for scanning the whole array for that value choosen
|
|
|
|
'-----------------
|
|
' static array
|
|
MySingleArray(4) = 4
|
|
MySingleArray(8) = 8
|
|
For n = 1 To 10
|
|
If MySingleArray(n) > 0 Then Print MySingleArray(n)
|
|
Next
|
|
|
|
|
|
'dynamic array
|
|
|
|
WhoIs(-10).Name = "QB64"
|
|
WhoIs(10).Name = "C#"
|
|
WhoIs(1).Name = "Java"
|
|
Print WhoIs(-10).Name, WhoIs(10).Name, WhoIs(1).Name
|
|
ReDim _Preserve WhoIs(-10 To 19) As MyType
|
|
Print
|
|
Print WhoIs(-10).Name, WhoIs(10).Name, WhoIs(1).Name
|
|
Print WhoIs(-1).Name, WhoIs(19).Name, WhoIs(10).Name
|