This task is about arrays.

For hashes or associative arrays, please see Creating an Associative Array.

For a definition and in-depth discussion of what an array is, see Array.



Task

Show basic array syntax in your language.

Basically, create an array, assign a value to it, and retrieve an element   (if available, show both fixed-length arrays and
dynamic arrays, pushing a value into it).

Please discuss at Village Pump:   Arrays.

Please merge code in from these obsolete tasks:

   Creating an Array
   Assigning Values to an Array
   Retrieving an Element of an Array


Related tasks

   Collections
   Creating an Associative Array
   Two-dimensional array (runtime)


 
Module CheckArray {
      \\ Array with parenthesis in name
      Dim A(10)=1
      Global B(10)=1
      For This {
            Local A(10)=5
            Print A(4)=5
      }
      Print A(4)=1
 
      \\ Auto Array
      M=(1,2,3,4,5)
      Link M to M()
      Print M(2)=3
      Return M, 0:=100, 5-4:=300
 
      \\ Retrieve an Element of an Array
      k=Each(M, 1, 2)
      \\ print 100 300
      While k { Print Array(k),}
      Print 
      Print Array(M, 2)=3
      Print Array("M", 2)=3
      Print Array(B(), 1)=1
      \\ arrays are containers for every value/object/pointer
      B(0):="Hello",100,"Good Morning", 200
      \\ using set to make B$() global too
      Set Link B() to B$()
      Print B$(0), B(1), B$(2), B(3)
      Swap B(0), B(2)
      Swap B(1), B(3)
      Print B$(0), B(1), B$(2), B(3)
      Print B()
      \\ Reduce B() to 4 elements - and change dimensions
      \\ we have to redim the global array, using set to send line to console
      \\ all globals are part of level 0, at console input.
      Set Dim B(4)
      Module CheckGlobal {
            Print B$(0), B(1), B$(2), B(3)
      }
      CheckGlobal
      Print B()
      Dim BB(4)
      \\ Copy 4 items from B() to BB(), from B(0), to BB(0)
      Stock B(0) keep 4, BB(0)
      Link BB() to BB$()
      Print BB$(0), BB(1), BB$(2), BB(3)
      \\ Arrays of structures in Buffers
 
      Structure TwoByte {
            {
                  ab as integer
            }
            a as byte
            b as byte
      }
      Print Len(TwoByte) = 2
      \ Use clear to clear memory
      \\ Mem is a pointer to a Buffer object
      Buffer Clear Mem as TwoByte*20
      Print Len(Mem)=40
      Return Mem, 0!ab:=0xFFAA
      Print Eval(Mem, 0!a)=0xAA,  Eval(Mem, 0!b)=0xFF
      Return Mem, 0!b:=0xF2
      Hex Eval(Mem,0!ab)   ' print 0xF2AA
      \\ Redim with preserve
      Buffer Mem as TwoByte*40
      \\ copy 40 bytes  at index 20 (40 bytes from start)
      Return Mem, 20:=Eval$(Mem, 0, 20*2)
      Hex Eval(Mem,20!ab)   ' print 0xF2AA
      A(3)=Mem
      Hex Eval(A(3),20!ab)   ' print 0xF2AA
      \\ now Mem change pointer
      Clear Mem
      Print Len(Mem)
      \\ old Mem is in A(3)
      Hex Eval(A(3),20!ab)   ' print 0xF2AA
      \\ we can change 
      Buffer Clear Mem as Integer * 200
      Print Len(Mem)=400
      Return Mem, 0:=Eval$(A(3), 0, 80)
      Hex Eval(Mem,20)   ' print 0xF2AA
      \\ change type without use of clear
      Buffer Mem as TwoByte * 200
      Hex Eval(Mem,20!ab)   ' print 0xF2AA
}
CheckArray
 