From: http://www.lua.org/pil/11.html
Lua
11 – Data Structures
Tables in Lua are not a data structure; they are the data structure. All structures that other languages offer---arrays, records, lists, queues, sets---are represented with tables in Lua. More to the point, tables implement all these structures efficiently.
In traditional languages, such as C and Pascal, we implement most data structures with arrays and lists (where lists = records + pointers). Although we can implement arrays and lists using Lua tables (and sometimes we do that), tables are more powerful than arrays and lists; many algorithms are simplified to the point of triviality with the use of tables. For instance, you seldom write a search in Lua, because tables offer direct access to any type.
LISTrace = {
human = { race = human, hp = 50, maxhp = 50, str = 4, atk = 5},
foxdemon = { race = fox, hp = 75, maxhp = 75, str = 3, atk = 4},
catdemon = { race = cat, hp = 60, maxhp = 60, str = 2, atk = 4},
}
<HumanRace>race=human, maxhp=50, speed=10, stamina=45</HumanRace>
<DogRace>race=dog, maxhp=15, speed=20, stamina=20</DogRace>
<CatRace>race=cat, maxhp=8, speed=15, stamina=18</CatRace>
dictionary add (choices, game.races.HumanRace, "Human")
dictionary add (choices, game.races.CatRace, "Cat")
dictionary add (choices, game.races.DogRace, "Dog")
<object name="Races">
<HumanRace>race=human, maxhp=50, speed=10, stamina=45</HumanRace>
<DogRace>race=dog, maxhp=15, speed=20, stamina=20</DogRace>
<CatRace>race=cat, maxhp=8, speed=15, stamina=18</CatRace>
...
</object><game ...>
....
<races type="object">Races</races>
...
</game><!--Saved by Quest 5.2.4515.34846-->
<asl version="520">
<include ref="English.aslx" />
<include ref="Core.aslx" />
<game name="ApplyAttributes">
<gameid>e4c54dda-b7a0-470f-abbc-2d603f94a18e</gameid>
<version>1.0</version>
<start type="script">
choices = NewStringList()
races = NewObjectList()
list add (races, HumanRace)
list add (races, CatRace)
list add (races, DogRace)
foreach (race, races) {
list add (choices, race.name)
}
msg(" ")
show menu ("What is your race?", choices, false) {
// Copy all the attributes, using the "attributes" string list as key.
ApplyAttributes(player, GetObject (result))
msg("player.race = " + player.race)
msg("player.maxhp = " + player.maxhp)
msg("player.speed = " + player.speed)
msg("player.stamina = " + player.stamina)
}
</start>
</game>
<object name="HumanRace">
<stats type="stringdictionary">race=Human; maxhp=50; speed=10; stamina=45</stats>
</object>
<object name="DogRace">
<stats type="stringdictionary">race=Dog; maxhp=15; speed=20; stamina=20</stats>
</object>
<object name="CatRace">
<stats type="stringdictionary">race=Cat; maxhp=8; speed=15; stamina=18</stats>
</object>
<object name="room">
<inherit name="editor_room" />
<object name="player">
<inherit name="defaultplayer" />
</object>
</object>
<function name="ApplyAttributes" parameters="object, race"><![CDATA[
foreach (attribute, race.stats) {
value = StringDictionaryItem (race.stats, attribute)
if (IsNumeric(value)) {
set (object, attribute, ToInt(value))
} else {
set (object, attribute, value)
}
}
]]></function>
</asl>