TGC Codebase Backup



Using an array as a list by IanM

4th Dec 2003 17:29
Summary

Simple readable example of using an array as a list



Description

Simple readable example of using an array as a list



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    ` Demonstration of the list commands
`
` ARRAY INSERT AT TOP
`  - Adds a new item to the top of the array and sets the current index
`    to point to that item.
` ARRAY INSERT AT BOTTOM
`  - Adds a new item to the end of the array and sets the current index
`    to point to that item.
` ARRAY DELETE ELEMENT
`  - Removes the current item
` PREVIOUS ARRAY INDEX/NEXT ARRAY INDEX
`  - Move the current index point back/forward through the list
` ARRAY INDEX TO TOP/ARRAY INDEX TO BOTTOM
`  - Position the array index point to the top or bottom of the list
` ARRAY INDEX VALID
`  - Determine whether we have moved of either end of the list
`
` I use ArrayName() to access the current item (eg to set the values
`                       in the item just added to the stack, and to
`                       read the value at the top of the stack).

` Several things to note here:
`   Adding to the list automatically increases the size of the array.
`   Removing from the list automatically decreases the size of the
`     array.
`   You cannot create an empty array (at the moment - Lee may include
`     it in Update 6) so the array needs to be cleared before you use
`     it.
`   Deleting the current item will automatically move you forward
`     onto the next item.
`   You cannot insert at the 'current' position, unless you know the
`     number of the item you are positioned at. You can do this by
`     using my array plug-in function GET ARRAY INDEX()

dim a() as integer
empty array a()

print "Add 5 items to the list"
for i=1 to 5
   n = rnd(1000)
   print "  Adding "; n; " to the list"
   array insert at bottom a()
   a() = n
next i

print
PrintList()

print
print "Run through the list forwards"
array index to top a()
while array index valid( a() )
   print a()
   next array index a()
endwhile

print
print "Run through the list backwards"
array index to bottom a()
while array index valid( a() )
   print a()
   previous array index a()
endwhile

print
print "Step forward to the third item and delete it"
array index to top a()
next array index a()
next array index a()
array delete element a()
PrintList()

print
print "Now insert 3 new random numbers at the top of the list"
for i=1 to 3
   n = rnd(1000)
   print "  Adding "; n; " to the list"
   array insert at top a()
   a() = n
next i
print
PrintList()

print
print "Deleting every item under 500"
array index to top a()
while array index valid( a() )
   if a() < 500
      array delete element a()
   else
      next array index a()
   endif
endwhile
PrintList()

wait key
end

function PrintList()
   if array count( a() ) >= 0
      print "The list has "; array count( a() )+1; " items"
      for i=0 to array count( a() )
         print "  Item "; i; " = "; a(i)
      next i
   else
      print "The list is empty"
   endif
endfunction