6 Replies Latest reply on May 23, 2007 6:05 AM by James Newton, ACP

# List Function Syntax

I feel so stupid asking some of these questions because I just came out of a semester of Director MX2004 and I feel like I should know this...but it really wasn't addressed in class.

I'm trying to make Director check a list for a set of items. For example, a list of numbers [1,3,4,1,1,6]. I want the program to check for a specific set of numbers [1,1,1] so that it will trigger a specific response. I know how to trigger the response using an on mouseUp handler, but I can't figure out how to make it check for the sets of numbers I need.

It's a dice game. The player uses a mouse click to roll 6 individual dice. The results (like 1,3,4,1,1,6) show up in text fields above the roll button. The player then chooses which dice to keep. From what was kept, the program figures a score and then (if applicable) gives the player a chance to roll again with the remaining dice. What the player keeps from the second roll is added to the first score.

I can make it roll [random(6)] and put the result into the field and I can even make it add all the field contents to a list...but I can't make it check the list for patterns.

Am I making any sense at all? I feel like someone's going to say "go back to the sandbox, little girl, you don't have enough experience to be here" but I'm too frustrated not to ask.

Taking a Risk,
• ###### 1. Re: List Function Syntax
Relax, we're friendly. It's a perfectly resonable question and well asked. Director doesn;t have a simple function to do what you need, but some simple coding can achieve it.

Firstly, in your example, does the order matter? So when you want to find [1, 1, 1], should [1, 3, 4, 1, 1, 6] trigger a match or not? If order doesn't matter then the first thing you should do is sort the list so that you have [1, 1, 1, 3, 4, 6] (see Sort command in help).

To do the actual pattern matching, you could use repeats to walk through the lists, but this could easily get complicated. Easier is to actually convert the list to a string and use the contains command.

myList = [1, 3, 4, 1, 1, 6]
sort myList --if order is unimportant
myString = ""
repeat with i in myList
put string(i) after myString
end repeat
match = myString contains "111"
put match

The above is really basic just to show what I mean, you'll need to play around with it of course.

hth

johnAq
• ###### 2. List Function Syntax
That makes sense. So...contains can only be used for a string, not a numeric? *chuckle* that could be why I'm having so much trouble making it work for the numbers.

So, thinking out loud here, I convert the numbers to string variables...I don't really have to use numbers at all in the coding, the player won't see that anyway and I can match each string variable (A, B, C...) to a number for the adding up that the player needs to see.

The game allows the player to accumulate a score during their turn. At some point they will decide to keep their score and pass the dice to another player or they will bust and lose all their accumulated points. If they keep their score, it's added to their game score (as opposed to turn score) and if they bust, 0 is added to their game score. I need to allow the player to choose which dice to keep while also making sure the game recognizes the point values of the kept dice and also recognizes when the player has rolled a combination which has no point value (bust).

I'm trying to break down the development process into little bitty bites. It's a little less overwhelming that way and I have a better chance of actually finishing the darn thing! So, right now I'm just working on the list aspect of it. I'll stop chewing your ear and go play with your suggestion. It looks intriguing and makes some things make sense that didn't before.

Thanks!!
serenity

P.S. No, order doesn't matter. In fact for the wetware, reordering the list (1,1,1,3,4,6) makes the combinations much easier to see. When we play this game with 3D dice that's one of the ways we rule a turn.
• ###### 3. Re: List Function Syntax
Yes, you could almost do away with the lists, but it probably makes sense for the ease of sorting.

hth

johnAq
• ###### 4. Re: List Function Syntax
Lists are fast and very powerful. I would recommend that you persevere with understanding how lists work and how to get the most out of them.

on FindPatternInList(aList, aPattern) --------------------------------
-- INPUT: <aList> and <aPattern> should both be lists
-- OUTPUT: Returns TRUE if all the items in aPattern are found in
-- aList.
-- SAMPLE: put FindPatternInList([1,3,4,1,1,6], [1,1,1])
-- -- 1
-- put FindPatternInList([1,3,4,1,1,6], [1,2,3])
-- -- 0
---------------------------------------------------------------------

-- We are going to delete items in aList, but lists are passed by
-- reference. If we delete items in the original list, they will
-- be deleted everywhere the list is used, including in the calling
-- handler. To avoid this, we first duplicate the list. This
-- copies an identical list to a different memory address in RAM.
-- We can use the same variable name: it will simply point to the

--aList = aList.duplicate()

repeat with vItem in aPattern
vIndex = aList.getPos(vItem)
if not vIndex then
-- This item is missing from aList. There is no match.
return FALSE
end if

-- If we get here, the current item in aPattern also occurs in
-- aList. Delete the item from aList so that it will not be
-- used for a subsequent match for a similar item

aList.deleteAt(vIndex)
end repeat

return TRUE
end FindPatternInList

To understand my point about duplicates and memory addresses, try the following in the Message window:

vList = [1,3,4,1,1,6]
put FindPatternInList(vList, [1,1,1])
-- 1
put vList
-- [1,3,4,1,1,6]

Now comment out the line "aList = aList.duplicate()" and recompile the script. Try the same series of instructions again:

vList = [1,3,4,1,1,6]
put FindPatternInList(vList, [1,1,1])
-- 1
put vList
-- [3, 4, 6]

Notice how the three items with a value of 1 have been deleted from the original vList.

The fact that you can pass a list to a handler and have its contents modified is essential to understanding what a list is and why lists work so fast.
• ###### 5. Re: List Function Syntax
Yes!!
Ok...I understood the concept you explained...but not most of the terminology. It only means I need to study lists more. I have three books on Director, one which we used for the class I took and which doesn't even mention lists because it doesn't really do much with Lingo at all, and two loaned to me by my instructor which only have limited information on lists. I found another one on SafariBooks, online which seems to be a little better.

I haven't had a chance to work much with the problem since the last time I posted here...it's complicated...but the idea that I can dupe a list and then delete what I can't use out of it (anything that doesn't match the pattern) is exactly what I need.

*chuckle* I'm excited and I can't wait to get back to programming!! I'll let you know how it goes when I can get back to it. I should have my server up and running by next week, so I can show you what I'm working on better then.

Thank you!!

Like a kid in a candy shop,
serenity
• ###### 6. Re: List Function Syntax
Handler: a separate chunk of code that you can execute. In other programming languages it might be called a method or a function.

Parameter: an item of data that you send to a handler. In other languages, this might be called an attribute.

In my example "FindPatternInList()" is a handler, and "aList" and "aPattern" are parameters.

Depending on what type of data you pass as a parameter, the data may be the original data or a copy. The technical terms are "reference" and "value". If you pass a list as a parameter to a handler, Director will pass the original list. Any changes to that list in the handler will affect the list anywhere else it is referred to. Lists are "passed by reference".

Strings, on the other hand, are "passed by value". Director will make a copy of the string and the handler will work on that copy. The original string will not be affected.

Suppose you want to change the original string. In that case, you must return the modified copy. Example:

aString = "This is "
aString = Concatenate(aString, "a string") -- the equals sign places a new value in aString
put aString
-- "This is a string"

The Concatenate() handler might look like this:

on Concatenate(aString1, aString2)
put aString2 after aString1
return aString1
end

If you comment out the line "return aString1" above, then aString will become <Void>, as no value will be returned from the Concatenate() handler.

Variable names (and parameters are variables) are merely human-readable labels that refer to the place in the computer's Random Access Memory (RAM) where the beginning of a particular piece of data is stored. When you pass a list as a parameter, Director actually passes this memory address. You can thus have two or more variables which "point" to the same chunk of memory. If you use one variable to change the data at that memory address, the other variable will instantly see the same change.

This is true of all "objects": lists, points, rects, instances of Parent Scripts, images, sprites, members, colors and so on are all objects.

Strings, floating point numbers and integers are not objects. They are "primitives". When a primitive is passed to a handler, Director copies the data from its current memory address and creates a new copy at a new memory address. Because both objects and primitives look like variable names, it is not immediately evident whether the data is an object being passed by reference or a non-object being passed by value.