Articles Archive
Articles Search
Director Wiki

Searching through a property list

November 9, 1998
by Pat McClellan

Dear Multimedia Handyman,

What I need is a search engine of sorts, to search through my list of data. My list looks like this:

gDataList=[[#name:"Amanda", #surname:"Allard", #department: "banking", #Phone:"3477"], [#name:"Wendy", #surname:"Allen", #Department:"Executive", #Phone:"7777"], [#name:"Peter", #surname:"Jedrasicak", #department:"B.I.S", #Phone:"7014"], [#name:"Neil", #surname:"Cooper", #department:"Marketing", #Phone:"7012"]]

The user has to fill in a First Name, and Last Name field, then press a Search button.



First, let me credit Tab Julius' book "Lingo!" for teaching me most of what I know about lists. It's a great book.

Searching through lists is always faster if you sort the list. Once you've sorted a list, it will always remain sorted. Sorting is comparable to inserting alphabetical dividers in a card catalog -- it allows you to not search through records you know don't apply. The trick is to create a useful index by which to sort -- and later, by which to search.

To sort your list, you'll need to make it a compound property list, indexed by some unique value in each record of the list. Note: you cannot repeat the index. So, indexing by first name is bad because you're likely to have 2 or more Johns or Bills or Amandas. Indexing by last name isn't much better because you can't be assured that you will have unique values.

Since your user will be inputting first and last name, I'll assume that a combination of lastname and firstname will be unique -- figuring that if there is a coincidence of names at an office one of the "Bill"s would be listed as a William. We'll index by lastname and firstname, turning them into a combination symbol... like this:

set gDataList= [#JonesBill: [#name: "Bill", #surname: "Jones", #dept: "Executive", #Phone: "7777"], #AllenWendy: [#name: "Wendy", #surname: "Allen", #dept: "Executive", #Phone: "1234"], #JedrasicakPeter: [#name: "Peter", #surname: "Jedrasicak", #dept: "BIS", #Phone: "7014"]]

Now, to sort the list, simply add the line of code "sort gDatalist". This will sort the list alphabetically, AND the list will stay sorted as you add new entries.

sort gDataList
put gDataList

-- [#AllenWendy: [#name: "Wendy", #surname: "Allen", #dept: "Executive", #Phone: "1234"], #JedrasicakPeter: [#name: "Peter", #surname: "Jedrasicak", #dept: "BIS", #Phone: "7014"], #JonesBill: [#name: "Bill", #surname: "Jones", #dept: "Executive", #Phone: "7777"]]

Hey, look, alphabetical order by both lastName and firstname, just like we need. Now, if your user inputs lastname = "Jones" and FirstName = "Bill" into fields called "inputLast" and "inputFirst", and then pressed the Search button...

on mouseUp
  set comboname = the text of field "inputLast" & the¬
    text of field "inputFirst"
  set lookup = symbol(comboname)
  if voidP(findPos(gDataList,lookup)) then
    alert "This person is not in the database."
    set myDataList = getaProp (gDataList, lookup)
    -- very fast
    put the name of myDataList into field ¬
    put the surname of myDataList into field ¬
    -- etc.
  end if

VERY fast because it's looking through a sorted list. Therefore, it can find its place almost immediately.

NOTE: if the user enters a space or Return or Enter, it will screw up the lookup. Therefore, put a keyDownScript that ignores spaces & such...

on startMovie
  set the keyDownScript to "filterSpaces"
on filterSpaces
  case the key of 
    " ", ENTER, RETURN:
  end case

Good luck. If your data becomes large, check out the V12 Database Xtra.

Patrick McClellan is Director Online's co-founder. Pat is Vice President, Managing Director for Jack Morton Worldwide, a global experiential marketing company. He is responsible for the San Francisco office, which helps major technology clients to develop marketing communications programs to reach enterprise and consumer audiences.

Copyright 1997-2019, Director Online. Article content copyright by respective authors.