Lingo 4.3: Lists
READING: Manual pages of the commands described here

File "listexample.dir"

Lingo provides a powerful set of list manipulation commands. Here is an example that shows how to add items into a list. First, create the global list that the objects will be placed into:

on startMovie
  global mylist
  mylist = list()   -- same as mylist = []
end
Then, attach a script to each cast member so that, when the user clicks on the sprite, that object will be added (append) to the global list.
on mouseUp
  global mylist
  mylist.append("eye")
end
Look in the Message window as you click on each item and you will see what is added to your list. There is a list of list functions accessible via the Script window in Director (e.g. append, deleteOne, sort, setProp, etc.)

You can, of course, append and delete items from the list by executing code in the message window. For example, you could add and then delete an ear:

mylist.deleteOne("ear")
You could also find how many items are in a list and the list element at any position:
put mylist.getAt(1) -- same as mylist[1]
put mylist.count    -- same as count(mylist)
Now you can have a button, for example, that will always give you the last element in the list. (You have to name it something other than "last" becaust it is a Lingo keyword):
global mylast
mylast = mylist.count

put mylist.getAt(mylast) -- same as mylist[mylast]

These commands will be useful in your next assignment.

While you are looking at the lists, check the definitions of the commands
mylist.getLast() -- does as you expect
mylist.sort() -- returns 0 but alters the list!
mylist.min() -- does as you expect
mylist.max()
mylist.getOne(value) -- returns the INDEX of the value

 

Exercise: Program "listExercise"

First create a handler randomList(N) which takes as input a number N and fills up mylist with N random numbers drawn from the domain 1..N. You should work first with the Message window, but eventually create a button that does calls the handler randomList(10).

Next, create a handler scrambleList(mylist) which takes as input mylist and scrambles its contents in random locations within the list. It does not create a new list, but it scrambles the given ones. Again, you should work first with the Message window, but eventually create a button that does calls the handler.

 

 

Maintained By: Takis Metaxas
Last Modified: October 22, 2014