list/array - compare values

Hey all,

Does anyone have something like this:

ex.

list1: (unique values)
1
32
3
434
6
7

list2: corresponds with list1
1 >3434
32 >3
3 >3
434 >t
6 >e
7 >x

Help in this:

  1. get value from list1
  2. search that value in list2
    a. if list1 value is in list2 output the corresponding value
    ex.
    >list1: 1
    >list2: 1 > 3434
    >result is 3434
    b. if list1 value is NOT in list2 output NOT found

Issues:

  1. How can you find out what index is the value stored in without going through the entire list?
  2. Anyone have code or can provide a direction that can do the above?

TIA

Hi slashdot,

I know that someone will soon come up with a far more sophisticated solution, but until then code as follows might be sufficient :wink:


set listone to {1, 2, 3, 4, 5}
set listtwo to {{1, 444}, {3, 899}, {5, "t"}}

repeat with listoneval in listone
	repeat with listtwoval in listtwo
		set keyval to item 1 of listtwoval
		if (keyval as integer) = (listoneval as integer) then
			tell me
				activate
				display dialog "Found key " & listoneval & " in list two with value " & item 2 of listtwoval & "!"
			end tell
		end if
	end repeat
end repeat

Unfortunately AppleScript doesn’t support real key-value dictionaries (like e.g. NSDictionary), so we must use nested lists instead.

A quick attempt at using a handler:

set listone to {1, 2, 3, 4, 5}
set listtwo to {{1, 444}, {3, 899}, {5, "t"}}

repeat with x from 1 to count of listone
	set p to FindinList(item x of listone, listtwo)
	if p is false then display dialog (x & " Not Found") as string
end repeat

on FindinList(theitem, TheList)
	set founditem to false
	repeat with i from 1 to count of TheList
		if item 1 of item i of TheList is theitem then set founditem to item 2 of item i of TheList
	end repeat
	return founditem
end FindinList