-
Notifications
You must be signed in to change notification settings - Fork 0
API Table Functions
With the exception of PAIRS(), the following functions and the # operator apply only to tables that are indexed starting from 1 and do not have NIL entries. All other forms of tables can be considered as hash maps or sets, rather than arrays that have a length.
Add value VAL to the end of table TBL. Equivalent to:
TBL[#TBL + 1] = VAL
If index is given then the element is inserted at that position:
FOO={} -- CREATE EMPTY TABLE
ADD(FOO, 11)
ADD(FOO, 22)
PRINT(FOO[2]) -- 22
Delete the first instance of value VAL in table TBL. The remaining entries are shifted left one index to avoid holes.
Note that VAL is the value of the item to be deleted, not the index into the table. (To remove an item at a particular index, use DELI instead). DEL returns the deleted item, or returns no value when nothing was deleted.
A={1,10,2,11,3,12}
FOR ITEM IN ALL(A) DO
IF (ITEM < 10) THEN DEL(A, ITEM) END
END
FOREACH(A, PRINT) -- 10,11,12
PRINT(A[3]) -- 12
Like DEL(), but remove the item from table TBL at index I When I is not given, the last element of the table is removed and returned.
Returns the length of table t (same as #TBL) When VAL is given, returns the number of instances of VAL in that table.
Used in FOR loops to iterate over all items in a table (that have a 1-based integer index), in the order they were added.
T = {11,12,13}
ADD(T,14)
ADD(T,"HI")
FOR V IN ALL(T) DO PRINT(V) END -- 11 12 13 14 HI
PRINT(#T) -- 5
For each item in table TBL, call function FUNC with the item as a single parameter.
FOREACH({1,2,3}, PRINT)
Used in FOR loops to iterate over table TBL, providing both the key and value for each item. Unlike ALL(), PAIRS() iterates over every item regardless of indexing scheme. Order is not guaranteed.
T = {["HELLO"]=3, [10]="BLAH"}
T.BLUE = 5;
FOR K,V IN PAIRS(T) DO
PRINT("K: "..K.." V:"..V)
END
Output:
K: 10 v:BLAH
K: HELLO v:3
K: BLUE v:5
- ๐ Keys
- ๐ Hello World
- ๐พ Example Cartridges
- ๐ File System
โคด๏ธ Loading and Saving- ๐ Using an External Text Editor
- ๐ฝ Backups
- ๐ง Configuration
- ๐ธ Screenshots and GIFs
- ๐ Sharing Cartridges
- ๐ SPLORE
- ๐ผ๏ธ Sprite Sheet / Label (.png)
- ๐ต SFX and Music (.wav)
- ๐ค MAP and CODE
- ๐พ Cartridges (.p8, .p8.png, .p8.rom)
- ๐ Web Applications (.html)
- ๐ค Binary Applications (.bin)
- ๐น๏ธ Uploading to itch.io
- ๐พ Exporting Multiple Cartridges
- ๐ฅ Running EXPORT from the host operating system