Comining specific list items

Hi,

I have two lists:

set theLIST to {"1", "2", "3", "4", "5", "6", "7", "8"}
set sndLIST to {"A", "A", "A", "B", "C", "C", "C", "D"}

I need to combine items from both lists so the final list looks like this:

set shouldLookLikeLIST to {"A", "1", "2", "3", "B", "4", "C", "5", "6", "7", "D", "8"}

I have tried this so far without much success:

set theLIST to {"1", "2", "3", "4", "5", "6", "7", "8"}
set sndLIST to {"A", "A", "A", "B", "C", "C", "C", "D"}
set shouldLookLikeLIST to {"A", "1", "2", "3", "B", "4", "C", "5", "6", "7", "D", "8"}


set sorted to {}
set end of sorted to item 1 of sndLIST

repeat with i from 1 to count of theLIST
	set theCNT to count of theLIST
	if i < theCNT then
		set xx to item (i + 1) of sndLIST
		
		if xx is not equal to item i of sndLIST then
			--log xx
			set end of sorted to item i of sndLIST
			set end of sorted to item i of theLIST
		else
			set end of sorted to item i of theLIST
end if
		
	end if
end repeat
if item -2 of sndLIST is not equal to last item of sndLIST then
	set end of sorted to last item of sndLIST
	set end of sorted to item i of theLIST
	
end if
sorted
--result {"A", "1", "2", "A", "3", "B", "4", "5", "6", "C", "7", "D", "8"}

Don’t look at this script if you wanted the items in the order you described. Look at the scripts below posted by the experts.

set theLIST to {"1", "2", "3", "4", "5", "6", "7", "8"}
set sndLIST to {"A", "A", "A", "B", "C", "C", "C", "D"}
set newlist to {}
repeat with aitem in theLIST
	if aitem is not in sndLIST then
		set end of newlist to contents of aitem
	end if
end repeat
set shouldLookLikeLIST to {}
set newlist2 to newlist & sndLIST
repeat with aitem in newlist2
	if aitem is not in shouldLookLikeLIST then set end of shouldLookLikeLIST to contents of aitem
	
	
end repeat
shouldLookLikeLIST

If you use List and Record Tools osax then it can be as simple as

set shouldLookLikeLIST to union of theLIST and sndLIST with removing duplicates

Hi,

try this


set theLIST to {"1", "2", "3", "4", "5", "6", "7", "8"}
set sndLIST to {"A", "A", "A", "B", "C", "C", "C", "D"}

set sorted to {}
set lastLetter to ""

repeat with i from 1 to count of theLIST
	tell item i of sndLIST
		if it is not lastLetter then
			set end of sorted to it
			set lastLetter to it
		end if
	end tell
	set end of sorted to item i of theLIST
end repeat
sorted
--result {"A", "1", "2", "3", "B", "4", "C", "5", "6", "7", "D", "8"}

Try this:

set theList to {"1", "2", "3", "4", "5", "6", "7", "8"}
set sndLIST to {"A", "A", "A", "B", "C", "C", "C", "D"}
set newlist to {}

repeat with i from 1 to count of sndLIST
	if i > 1 then
		log i
		if item (i - 1) of sndLIST is not item i of sndLIST then
			copy item i of sndLIST to end of newlist
			copy item i of theList to end of newlist
		else
			copy item i of theList to end of newlist
		end if
	else
		copy item i of sndLIST to end of newlist
		copy item i of theList to end of newlist
	end if
	
	
end repeat

newlist

How about this

set theLIST to {"1", "2", "3", "4", "5", "6", "7", "8"}
set sndLIST to {"A", "A", "A", "B", "C", "C", "C", "D"}

set returnList to {}
set beginCurrentLetter to 1
set done to false

repeat until done
	set endCurrentletter to beginCurrentLetter
	
	set testVal to item beginCurrentLetter of sndLIST
	copy testVal to end of returnList
	
	repeat with i from beginCurrentLetter to count of sndLIST
		if item i of sndLIST ≠ testVal then
			set i to i - 1
			exit repeat
		end if
	end repeat
	set endCurrentletter to i

	set returnList to returnList & (items beginCurrentLetter thru endCurrentletter of theLIST)
	
	if (count of sndLIST) ≤ endCurrentletter then
		set done to true
	else
		set beginCurrentLetter to endCurrentletter + 1
	end if
end repeat

returnList
-- {"A", "1", "2", "3", "4", "C", "5", "6", "7", "8"}

Thanks Stefan,

That solved it.

the others solve it, too :wink:

Quite Right!

Thanks to everyone who sent a solution to this.:wink: