1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
.. _tables:
=================
Tables
=================
.. index::
single: Tables
Tables are associative containers implemented as pairs of key/value (called slot); values
can be any possible type and keys any type except 'null'.
Tables are squirrel's skeleton, delegation and many other features are all implemented
through this type; even the environment, where "global" variables are stored, is a table
(known as root table).
------------------
Construction
------------------
Tables are created through the table constructor (see :ref:`Table constructor <table_contructor>`)
------------------
Slot creation
------------------
.. index::
single: Slot Creation(table)
Adding a new slot in a existing table is done through the "new slot" operator ``<-``; this
operator behaves like a normal assignment except that if the slot does not exists it will
be created.::
local a = {}
The following line will cause an exception because the slot named 'newslot' does not exist
in the table 'a'::
a.newslot = 1234
this will succeed: ::
a.newslot <- 1234;
or::
a[1] <- "I'm the value of the new slot";
-----------------
Slot deletion
-----------------
.. index::
single: Slot Deletion(table)
::
exp:= delete derefexp
Deletion of a slot is done through the keyword delete; the result of this expression will be
the value of the deleted slot.::
a <- {
test1=1234
deleteme="now"
}
delete a.test1
print(delete a.deleteme); //this will print the string "now"
|