can someone explain this? alphabetical lists

Hi… I found this while doing a search on this board-- and I needed a way to take a large list and alphabetize it but add carriage returns after each line-- so I added the \n to it…

on sort_list(the_list) set nl to (ASCII character 10) tell (a reference to my text item delimiters) set {old_delim, contents} to {"\n", contents, nl} set {the_list, contents} to {"" & the_list, old_delim} end tell return paragraphs of (do shell script "echo " & (quoted form of the_list) & " | sort") end sort_list
Everything seemed great, except I started experiencing STRANGE behavior…

My assumption was that with this code, I could just say:

my sort_list(list1)
set list1 to the result
my sort_list(list2)
set list2 to the result

— what I found was that, all lists from then on were automatically being affected by sort_list…

I could just say:

my sort_list(list1)

and list2 would automatically be affected, and I was experiencing carriage returns in places where I didn’t even want them!!!

example, I then had:

set bla to count & " hello there, here is a list: " & list2 as string

– and I would get

72
hello there, here is a list:

apple
dog
grape
igloo

So, can someone please break this down for me-- tell me what is going on, and how can I avoid this?

and also-- is there a way to include case insensitivity to this sorting so that I can have

jason
Jerry Seinfeld
jimmy johnson
lucky
Man in the moon

etc…etc.etc

thank you!

When you ‘set’ a varible to a varible whose value is a list, they share values.

set a to {1, 2, 3}
set b to a
set end of b to 4
a

instead use ‘copy’.

copy a to b

gl,

Another thing that this happens with is subroutines.

set a to {1, 2, 3}
AddOne(a)
{a, result}

on AddOne(b)
set end of b to 4
return b
end AddOne

You can overcome this with contents:

set a to {1, 2, 3}
AddOne(contents of a)
{a, result}

on AddOne(b)
set end of b to 4
return b
end AddOne

Another way would be to not work on the passed variable:

set a to {1, 2, 3}
AddOne(a)
{a, result}

on AddOne(b)
copy b to tempv
set end of tempv to 4
return tempv
end AddOne

gl,

Check out the third line in your sort handler, Patrick. It attempts to set a 2-item list to one with 3 items. If you try something like that, the extra items are ignored:

set {a, b} to {"a", "b", "c"}
{a, b}
--> {"a", "b"}

That means your handler always sets the variable ‘old_delim’ to “\n” - and the text item delimiters to themselves.

Try something like this instead:

on sort_list(the_list)
	set nl to (ASCII character 10)
	tell (a reference to my text item delimiters)
		set {old_delim, contents} to {contents, nl}
		set {the_list, contents} to {the_list as string, old_delim}
	end tell
	paragraphs of (do shell script "echo " & (quoted form of the_list) & " | sort -f")
end sort_list

set list1 to {"lucky", "Jerry Seinfeld", "Man in the moon", "jimmy johnson", "jason"}
set list2 to {"hello there, here is a list:", "apple", 72, "igloo", "dog", "grape"}

set list1 to sort_list(list1)
set list2 to sort_list(list2)

{list1, list2}
--> {{"jason", "Jerry Seinfeld", "jimmy johnson", "lucky", "Man in the moon"}, {"72", "apple", "dog", "grape", "hello there, here is a list:", "igloo"}}

If you want the output as text, rather than a list, leave out the ‘paragraphs of’ coercion in the sort handler’s last line, thus changing it to: