Add a string to a list

I’ve searched but I can’t find an answer to this. I want to do something like the following, but it’s not working:


repeat with i from 1 to the number of items in xmlData
	if (item i of xmlData) contains "<string>" then
		add (item i of xmlData) to theList
	end if
end repeat

The problem is with the line “add (item i of xmlData) to theList”. I totally guessed at this line of code. All I want is to append the resulting string to the end of the list. When I try to execute I get the following error: “A to:theList can’t go after this add(item i of xmlData).”

I hope somebody can help.
Thanks
Mark

The process you want is called concatenation and the operator is “&”. Assuming theList really is a list then:

set theList to {"my", "dog", "has"}
-- to add "fleas"
set theList to theList & "fleas"

You can also set the end of the list:

set theList to {"my", "dog", "has"}
-- to add "fleas"
set the end of theList to "fleas"
theList

Hi Nova

Thanks for the info. I just figured it out using the copy command:

copy (item i of xmlData) to end of theList

I prefer to shorten it:

set theList's end to (item i of xmlData)

That’s the form I would use too, but I wanted to make it obvious.

Setting the end of a list is generally more efficient than “concatenating” items to it and should (in my view) be preferred ” unless there’s some reason for doing otherwise.

Nigel;

How do you find out this stuff - where can I read about it?

Hi, NovaScotian.

Most of my assertions above have been gleaned from hanging around on the AppleScript-Users mailing list. Apple’s main representative on the list, Chris Nebel, occasionally reveals a little of what goes on under the bonnet of the language in order to settle arguments going on in the list. He’s reluctant to give too much away in case it tempts people to write hacks that depend on that information rather than on the official language spec. But what he does reveal is often of interest to those of us who are interested in script efficiency. He has in the past explained the differences between concatenation and setting the end of a list and why ‘copy’ always take longer than set, even with non-mutable data.

Having reread the “concatenation” section in the AppleScript Language Guide today, I’m not so sure of my ground with regard to an item being coerced to list before being concatenated to one. But the fact that the result of a list concatenation is a new list is easily demonstrated by setting a different variable to it:

set theList to {1, 2, 3}
set newList to theList & 4

newList --> {1, 2, 3, 4}
theList --> {1, 2, 3}

However (if you’re interested) someone on the AS-Users list discovered an exception to this a few weeks ago. Probably a bug, it’s been around for years but no-one’s apparently noticed it before: if a list is concatenated to an empty list, the result is the concatenated list, not a new one.

set theList to {1, 2, 3}
set newList to {} & theList
set item 2 of newList to 7
theList --> {1, 7, 3}

There’s an analogous situation when concatenating a record to an empty record.