|
Finding indexes
You know how to find any entry by using the property/value pair. Now, how about going the other way? Given an entry, how can you find its index? Let's say that the list consists of customers and their requests. When customers tell you their names, you have to be able to say, "Yes, I see here that you are the fifth customer in line there are four people ahead of you".
You can do that with the findPos and getPos functions. The findPos function returns an index if you give it a property. The getPos function returns an index if you give it a value.
So, let's say your list contains customer names and phone numbers. It might look like this:
clientList
["O.N. Handler":"123-4567", "Billi Rubin":"555-1212",
"Sue Alawyer":"555-1234", "I. M. Reddy":"999-9876"]
If you are given a name, you can look up the name (which you use as the property) to find the index:
put findPos(clientList, "Sue Alawyer")
-- 3
It may be helpful to look by valuein this case, the phone numberas names may be difficult to spell exactly as you have them:
put findPos(clientList, "555-1234")
-- 3
Each of these functions returns an index. You've already used functions to return values when you know the property and properties when you know the index, so it's an easy step to find almost any item you want if you know at least one of its features.
Further, you can give a value and look up the property associated with the first occurence of a value by using the GetOne(list, value) function. On the above list, you can do the following:
put getOne(clientList, "555-1234")
-- "Sue Alawyer"
This returns the property that corresponds with a value. This is the opposite of getAProp() . Keep in mind that this function only returns the property associated with the first occurrence of that value. If two entries have the same phone number, only the first entry is returned. To get all of the entries, you'd have to traverse the list by index, getting each value by index and checking each value to see if it was the one you wanted.
|