tables.rst 1.5 KB

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