DekGenius.com
[ Team LiB ] Previous Section Next Section

15.5 Concatenation Operator

Concatenation may be performed on a pair of strings (resulting in a string), a pair of lists (resulting in a list), or a pair of records (resulting in a record). Implicit coercions are performed in exactly the same way as for the containment operators; see Section 15.4, just previously.

So, for example:

"three" & 20 -- "three20"
3 & "twenty" -- {3, "twenty"}

This shows the difference the order of operands can make; the reason is perfectly obvious if you know the implicit coercion rules, baffling otherwise.

To turn string concatenation into list concatenation, it suffices to coerce the first operand to a list; this can be done simply by expressing it in list delimiters. So:

{"Mannie"} & "Moe" & "Jack" -- {"Mannie", "Moe", "Jack"}

Without the list delimiters, we'd end up with "MannieMoeJack".

Recall (from Chapter 14) that coercion of a list to a string is another way to concatenate. Thus concatenation of a string and a list concatenates the string with all the elements of the list, each coerced to a string and joined by the text item delimiters:

set text item delimiters to ""
"butter" & {"field", 8} -- "butterfield8"

Recall what was said in the previous section (Section 15.4) about both operands having to be of the same type, and what this implies for lists. Concatenation is a way to append one or more items to a list:

{1, 2, 3} & {4, 5, 6} -- {1, 2, 3, 4, 5, 6}

The result is not {1, 2, 3, {4, 5, 6}}; if that's what you wanted, you can use an extra level of list delimiters:

{1, 2, 3} & {{4, 5, 6}}

Recall (from Chapter 13) that a more efficient way to append a single element to a list is like this:

set L to {1, 2, 3}
set end of L to 4 -- {1, 2, 3, 4}

The operation set end of is more efficient than the concatenation operator for lists, and coercion of a list to a string is more efficient than the concatenation operator for strings, because no extra copies have to be made internally. So, instead of this:

set s to "anti"
set s to s & "dis"
set s to s & "establishment"
set s to s & "arianism"

it is more efficient to say this:

set text item delimiters to ""
set L to {}
set end of L to "anti"
set end of L to "dis"
set end of L to "establishment"
set end of L to "arianism"
set s to L as string

Concatenating records yields a record consisting of all the items of the first record along with just those items of the second record whose name isn't the name of any item in the first record (see Chapter 13):

set r to {who:"Jaime", town:"Ojai"} & {who:"Matt", friend:"Steve"}
r -- {who:"Jaime", town:"Ojai", friend:"Steve"}

Scripting additions can provide further interesting variations on the notion of concatenation. For example, the Satimage scripting addition's special concat command concatenates lists from items with the same name in different records:

special concat {who:{"Matt"}} with {who:{"Neuburg"}}
-- {who:{"Matt", "Neuburg"}}
&concatenation

Syntax

string1 & string2
list1 & list2
record1 & record2

Description

The result is a string, list, or record respectively.

    [ Team LiB ] Previous Section Next Section