[12.1] What are lists good for anyway?

Lists allow you to collect and organize multiple data by various methods and treat the whole collection as a single item. A single list can contain items of different types, including other lists and child or factory objects.

You can arrange data by its position or order, like an array:

  set prizes = [ "Ferrari", "TV", "cornflakes" ] 
  set yippee = "You've won the" && getAt( prizes, random(3) )
Or, you can refer to it with labels, like in a C struct or Pascal record:
  set things = [ #number : 17, #name: "LaVerne", #list: [1,2,3] ] 
  set myNumber = getAProp(things, #number)
You can add things to a list at the beginning, or in a specific location, or delete them or interrogate them according to a whole range of criteria. You can also sort them, count how many items they contain, and so on.

Because items can be added to them and extracted from them according to the values in variables, their content can be varied at runtime, so you can create complex collections of data that vary according to what the user chooses, or the colours of his/her monitor, or the phases of the moon. You aren't limited to the things you've actually placed in your scripts.

Lists are a good way of keeping track of things that might vary in number (such as which sprite channels are currently occupied), or of things that need to be linked together (such as a person's name and address).

They also provide a means of simplifying and generalizing complicated scripts by moving specifics out of the lingo code itself and into a single data structure that can be easily modified. Consider the following handler:

on mouseUp
  put getHotspot() into hotSpot
  if hotSpot = #noHotSpot then beep 1
  else if hotSpot = #quitBtn then doQuit
  else if hotSpot = #helpBtn then doHelp
  else <etc>
end mouseUp
If there are a dozen different possible hotspots this quickly gets out of hand. If, alternatively, you create a global list of hotSpots and the actions to take for each one, like this:
  global gHotActions

  set gHotActions = [ #noHotSpot: "beep 1", #quitBtn : "doQuit", <cont>
  #helpBtn : "doHelp" <etc> ]
Then the mouseUp script only needs to look up the correct action in the list, and do it:
on mouseUp
  do getAProp( gHotActions, getHotSpot() ) 
end mouseUp
If you want to add another hotspot, you just need to add it to the list. You don't have to change your scripts at all. You could even allow the user to define additional hotspots and assign actions to them, just by adding them to the list dynamically (though you'd also have to have a list defining which hotspots are which for use by the getHotSpot() function...).

In short, lists are extremely versatile and general structures which you can probably put to good use in almost any situation. And as if that weren't enough, they're also rather speedy.