Section 15.6. Concatenation Operator


15.6. Concatenation Operator

Concatenation is the joining of two things in sequence. It 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 the previous section). So, for example:

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

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

In earlier versions of AppleScript, concatenation of Unicode text and a string was troublesome, because the class of the result depended the class of the first operand. Now (starting in Tiger), if either operand is Unicode text, the result is Unicode text. This behavior makes string concatenation effectively transparent.

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:

 {"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 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}} 

Concatenation of a list to the empty list yields exactly the same list, not a copy. This is probably intended as an optimization, but you are more likely to experience it as a bug. For example:

 set L1 to {} set L2 to {"mannie"} set L1 to L1 & L2 set item 1 of L1 to "moe" L2 -- {"moe"} 

Evidently L1 has been set (by reference) to L2; L1 and L2 are now two names for the same thing. This would not have happened if the first operand in the concatenation had originally been anything other than an empty list. One wants to say, "So just don't do that," but the problem is that if (as in this example) the first concatenation operand is a variable, you might not know that it is an empty list, and it seems onerous to suggest that you should test it every time. A workaround is to use copy instead of set in the third line:

 copy L1 & L2 to L1 

The operation set end of is more efficient than the concatenation operator because no extra copies have to be made internally. Strings are not mutable in place, but lists are. Thus it is common practice to perform an extended series of string operations by operating on a list instead. For example, 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"} 

Concatenating a record with an empty list (in either order) yields the same record. For a use of this, see "Parameters" in Chapter 9. If the empty list is the first operand, the result is the very same record, just as we saw a moment ago for lists:

 set R1 to {} set R2 to {name:"Matt"} set R1 to R1 & R2 set name of R1 to "Neuburg" R2 -- {name:"Neuburg"} 

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

concatenation

Syntax

 string1 & string2 list1 & list2 record1 & record2

Description

Concatenates the operands. The result is a string, list, or record respectively.




AppleScript. The Definitive Guide
AppleScript: The Definitive Guide, 2nd Edition
ISBN: 0596102119
EAN: 2147483647
Year: 2006
Pages: 267
Authors: Matt Neuburg

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net