DekGenius.com
[ Team LiB ] Previous Section Next Section

15.4 Containment Operators

Containment may apply to two strings, two lists, or two records. The result is a boolean. Containment implies comparison, and the nature of comparisons involving strings can be influenced by a considering clause; see Chapter 12.

The fact that in the case of list containment both operands must be lists is a little counterintuitive at first. Thus:

{1, 2} contains {2} -- true

You might have expected to say:

{1, 2} contains 2 -- true

You can say that, but only because 2 is coerced to {2} implicitly. In other words, the second operand is not an element; it's a sublist. Thus you can ask about more than one element at once. For example:

{1, 2, 3} contains {2, 3} -- true

Lists are ordered, so the items of the sublist you ask about must appear consecutively and in the same order in the target list; these are false:

{1, 2, 3} contains {1, 3} -- false
{1, 2, 3} contains {3, 2} -- false

Since lists can contain lists, you may have to use an explicit extra level to say what you mean:

{{1}, {2}} contains {2} -- false
{{1}, {2}} contains {{2}} -- true

The first is false because 2 is not an element of the first list, and {2} is not going to be coerced to {{2}} for you—it's a list already so there's nothing to coerce.

In the case of record containment, both the label and the value must match for containment to be true. So:

{name:"Matt", age:"49"} contains {name:"Matt"} -- true
{name:"Matt", age:"49"} contains {title:"Matt"} -- false
{name:"Matt", age:"49"} contains {name:"Socrates"} -- false

Records are not ordered:

{name:"Matt", age:"49"} contains {age:"49", name:"Matt"} -- true

Since the containment operators are overloaded to apply to both strings and lists, the first operand is never implicitly coerced to a string, because AppleScript can't know that this is what you mean; it is coerced to a list unless it is a string. The second operand is then coerced to match the datatype of the first.

So, for example:

"49" contains 4 -- true; string containment, "49" contains "4"
49 contains 4 -- false; list containment, {49} doesn't contain {4}

It's important not to confuse the implicit coercion rules here with those for certain other operators. For example, your experience with arithmetic operators might lead you to expect a certain kind of implicit coercion:

{"7"} * 7 -- 49

The list of a single string is coerced to a single string and from there to a number. But that isn't going to happen with contains:

{"7"} contains 7 -- false

The first operand isn't coerced at all; the second operand is coerced to {7}, and that's the end. The second operand isn't a sublist of the first, so the comparison is false.

contains, does not contain, is in, is not incontainment

Syntax

string1 contains string2
string2 is in string1
list1 contains list2
list2 is in list1
record1 contains record2
record2 is in record1

Description

The is in synonyms reverse the operand order of the contains synonyms—that is, with is in, the second operand comes before the first operand (as deliberately shown in the syntax listings). This is relevant in the rules for implicit coercions.

begins withinitial containment

Syntax

string1 begins with string2
list1 begins with list2

Description

Same as contains with the additional requirement that the second operand come first in the first operand. Records are not ordered, so they aren't eligible operands. Synonym is starts with.

ends withfinal containment

Syntax

string1 ends with string2
list1 ends with list2

Description

Same as contains with the additional requirement that the second operand come last in the first operand. Records are not ordered, so they aren't eligible operands.

    [ Team LiB ] Previous Section Next Section