Re: My Worrisome Lists :-)
Re: My Worrisome Lists :-)
- Subject: Re: My Worrisome Lists :-)
- From: email@hidden
- Date: Mon, 4 Feb 2002 21:16:14 -0500
On Mon, 04 Feb 2002 16:28:58 -0500, Stephen Swift <email@hidden>
asked,
>
Say I get a value of "hello" returned from my script. I want to add it to
>
my list of records *only* if the record hello doesn't already exist.
I got a little confused the first time, and we need to rephrase the question to
use standard AppleScript terms.
What you call a record is a 'property'. What you call a list is a record. That
is,
{hello:"goodbye",Brian:5,jane:6,leslie:{2,3}}
is a record, containing 4 properties: hello, Brian, jane, and leslie.
To add properties to a record that may already have them, you can use plain
record concatenation. If you concatenate two records that have some set of
property names in common, the contents of the left list rules. That is,
{ name : "Mr. Gumby", shoesize: 8 } & { shoesize : 9, hat : "hanky" }
--> result: { name : "Mr. Gumby", shoesize : 9, hat : "hanky" }
So if you want the new value to be put in the record only if it doesn't already
exist, put it in on the right side of the concatenation, If you want the new
value to replace the old, put it on the left. For example,
{hello:"goodbye",Brian:5,jane:6,leslie:{2,3}} & {hello : "adios"}
--> result: {hello:"goodbye",Brian:5,jane:6,leslie:{2,3}} (unchanged)
{hello: "adios"} & {hello:"goodbye",Brian:5,jane:6,leslie:{2,3}}
--> result: {hello:"adios",Brian:5,jane:6,leslie:{2,3}} (property changed)
>
This would work *if* the two record's data were the same, but I think that
>
won't be the case.
>
>
set list1 to {hello:"goodbye"}
>
list1 contains ({{+class usrf;:{"hello", "goodbye"}}} as string as record)
>
>
How do I check if the list contains the record if the records have different
>
data?
"Contains" does the job if you are looking for a specific value:
{hello:"goodbye",Brian:5,jane:6,leslie:{2,3}} contains {Brian:5}
--> result: True
{hello:"goodbye",Brian:5,jane:6,leslie:{2,3}} contains {Brian:6}
--> result: False.
If you want to see if the property already exists, try concatenating a special
value on the right and see if it changes.
({hello:"goodbye",Brian:5,jane:6,leslie:{2,3}} & {Brian: missing value} )
does not contain {Brian: missing value}
--> Result: True
(It might be faster to do the following)
Brian of ({hello:"goodbye",Brian:5,jane:6,leslie:{2,3}} & {Brian: missing
value}) is not missing value
--> Result: True
>
>
Silly as it may sound, sometimes I wish there was a coercion string to
>
variable some days.
There is one: "run script yourString" returns the contents of the named
variable. :-)
You really want associative arrays. So do we all. Although you might consider
all storage of the script to be properties of the script, and so should expect
me["myvar"] to be the variable myvar.
Above, I'm using Javascript syntax for an associative array. What might the
AppleScript syntax be? Perhaps 'property "Brian" of myRecord'
--
Scott Norton Phone: +1-703-299-1656
DTI Associates, Inc. Fax: +1-703-706-0476
2920 South Glebe Road Internet: email@hidden
Arlington, VA 22206-2768 or email@hidden