Noob question about adding to end of list

Hi,
I thought I was doing everything right when I run the following script:


set ChxPageNumbersFolder to (choose folder) as string
tell application "Finder"
	set ChxWordDocs to every file of folder ChxPageNumbersFolder
end tell
repeat 2 times
	set ChxWordDocs to rest of ChxWordDocs
end repeat
repeat with i from 1 to (count of ChxWordDocs)
	set numbersList to {"1"}
	set thisFile to item i of ChxWordDocs
	tell application "Microsoft Word"
		
		activate
		
		open thisFile
		
		set myFooter to get footer section 1 of active document ¬
			index header footer primary
		set stNumber to starting number of page number options of myFooter as string
		close active document without saving
	end tell
	set the end of numbersList to stNumber
	
end repeat
numbersList

the result is {“1”, “76”} not a list of every starting page number. Where did I go wrong?

Never mind, figured it out, my bad.:cool:

try this instead


set ChxPageNumbersFolder to (choose folder) as string
tell application "Finder"
	set ChxWordDocs to every file of folder ChxPageNumbersFolder
end tell
repeat 2 times
	set ChxWordDocs to rest of ChxWordDocs
end repeat
repeat with i from 1 to (count of ChxWordDocs)
	set numbersList to {"1"}
	set thisFile to item i of ChxWordDocs
	tell application "Microsoft Word"
		
		activate
		
		open thisFile
		
		set myFooter to get footer section 1 of active document ¬
			index header footer primary
		set stNumber to starting number of page number options of myFooter as string
		close active document without saving
	end tell
	set numbersList to numbersList & stNumber
	
end repeat
numbersList

Browser: Firefox 2.0.0.8
Operating System: Mac OS X (10.5)

Hi, misterfriendly.

Paeon’s mistake was that he was setting numbersList to {“1”} inside the repeat instead of before it, so he was getting a new {“1”} with each iteration instead of continuing with the list to which he’d just added an item.

Your suggestion uses concatenation to grow the list. This can be useful sometimes, but isn’t as efficient as setting the end of the list to the required value.

set end of numbersList to stNumber
--> Appends stNumber to numbersList.
--> Result: the same physical list, but with an additional item.

set numbersList to numbersList & stNumber
--> stNumber is coerced to list too (if it isn't one already, which it isn't here)
--> and the result is concatenated to numbersList.
--> Result: a third list containing the same items as the other two
--> and whose length is the sum of the lengths of the other two.
--> The variable 'numbersList' is reassigned to this new list.

It should be obvious from this that the process of “growing a list” by concatenation involves the creation and discarding of progressively longer lists, which can begin to affect performance. (Technically, this also happens a little with the other method, but only when adding an item exceeds the list’s current memory allocation.)

Here are some ramifications of concatenating to a list:

{1, 2, 3, 4, 5} & 6 -- Integer coerced to list {6} before concatenation.
--> {1, 2, 3, 4, 5, 6}

{1, 2, 3, 4, 5} & {6} -- Integer already in a list.
--> {1, 2, 3, 4, 5, 6}

{1, 2, 3, 4, 5} & {6, 7, 8} -- Multi-item list.
--> {1, 2, 3, 4, 5, 6, 7, 8}

{1, 2, 3, 4, 5} & {{6, 7, 8}} -- Multi-item list in a list.
--> {1, 2, 3, 4, 5, {6, 7, 8}}

{1, 2, 3, 4, 5} & {a:9, b:10} -- Record coerced to list {9, 10} before concatentation.
--> {1, 2, 3, 4, 5, 9, 10}

{1, 2, 3, 4, 5} & {{a:9, b:10}} -- Record in a list.
--> {1, 2, 3, 4, 5, {a:9, b:10}}

Hope this is interesting. :wink: