Amending a list item

Hi all,
This seems really simple, but I can’t for the life of me work it out, and searching yields no answers.
I get a name of a spot colour and currently, if it is already in the list, adds an “L” on the end

set ColourList to {"0001", "0002", "0003", "0004"}
set aColour to "XXX 0001"
if (characters 5 thru 8 of aColour as string) is in ColourList then
	set end of ColourList to ((characters 5 thru 8 of aColour as string) & " L")
else
	set end of ColourList to (characters 5 thru 8 of aColour as string)
end if
return ColourList

…so now I have 2 instances of colour “0001”; “0001” and “0001 L” in my colourlist.

However, I need that first colour in the list to now be called “0001 T”. If only one instance of the colour exists, then it will not have a L or T suffix.

This:

repeat with ex_colours in ColourList
if ex_colours is equal to (characters 5 thru 8 of aColour as string) then
set ex_colours in ColourList to ex_colours & " T"
end if
end repeat

… of course doesn’t work, but is basically what I am after.

I would love to hear any ideas you might have on the matter :smiley:

Ian

Browser: Safari 525.13
Operating System: Mac OS X (10.4)

Is this along the lines of what you are looking for?

set ColourList to {"0001", "0002", "0003", "0004"}
set ex_colours to {"XXX 0001", "XXX 0003", "XXX 0005"}

set ex_colours_count to (count ex_colours)
repeat with i from 1 to ex_colours_count by 1
	set aColour to (characters 5 thru 8 of item i of ex_colours as string)
	if aColour is in ColourList then
		set ColourList_count to (count ColourList)
		repeat with c from 1 to ColourList_count by 1
			set aColourListColor to item c of ColourList
			if aColourListColor is equal to aColour then
				set item c of ColourList to aColourListColor & " L"
				set end of ColourList to aColour & " T"
				exit repeat
			end if
		end repeat
	else
		set end of ColourList to aColour
	end if
end repeat

James,

Thank you for your reply - that was exactly what I was looking for.
Actually I wasn’t really merging two lists. I already had all the colours in a raw format, so I have created the list already (with duplicates of colours in where appropriate) via a series of filters, and then your script example works on one full list and one empty list


set ColourList to {}
set ex_colours to {"XXX 0001", "XXX 0003", "XXX 0005", "XXX 0001"}

Here is that whole section of the script (in case it is of any use to anyone).


set text_to_list to {"X-00001-001", "00000000", "92345678", "XXX 0001", "XXX 0002", "XXX 0003", "XXX 0004", "XXX 0001"} --just a sample
set ColourList to {}
set colourstosort to (items 4 thru end of text_to_list) as list
repeat with aColour in colourstosort
	if aColour is not equal to "" then
		if aColour begins with "XXX" then
			set end of ColourList to (characters 5 thru 8 of aColour as string)
		else
			try
				if character 1 of aColour is in {"0", "1"} then
					set colstart to characters 1 thru 4 of aColour as list
					set mylst to {}
					repeat with thischar in colstart
						try
							thischar * 2  --check it's a number
							set end of mylst to 1
						end try
					end repeat
					if mylst is equal to {1, 1, 1, 1} then
						if (characters 1 thru 4 of aColour as string) ≠ "0000" then
							set end of ColourList to (characters 1 thru 4 of aColour as string)
						end if
					end if
				end if
			end try
		end if
	end if
end repeat
-- sort line and tone out -- James Nierodzik - Macscripter.net
set ex_colours to ColourList
set ColourList to {}
set ex_colours_count to (count ex_colours)
repeat with i from 1 to ex_colours_count by 1
	set aColour to (characters 1 thru 4 of (item i of ex_colours as string) as string)
	if aColour is in ColourList then
		set ColourList_count to (count ColourList)
		repeat with c from 1 to ColourList_count by 1
			set aColourListColor to item c of ColourList
			if aColourListColor is equal to aColour then
				set item c of ColourList to aColourListColor & " L"
				set end of ColourList to aColour & " T"
				exit repeat
			end if
		end repeat
	else
		set end of ColourList to aColour
	end if
end repeat

return ColourList

Thanks again :smiley:

Ian