Working with Lists

Get Unique Items In A List

on getUniqueItems(src)

	set srcCount to (count src)

	set unq to {}

	repeat with x from 1 to srcCount

		set srcItem to item x of src

		set unqCount to (count unq)
		set match to false

		repeat with y from 1 to unqCount
			set unqItem to item y of unq
			if srcItem = unqItem then
				set match to true
			end if
		end repeat

		if match is false then
			set end of unq to srcItem
		end if

	end repeat

	return unq

end getUniqueItems

Get Ranges of Numbers in a Sorted List

on CreateNumberRanges(sourceList)
	set itemCount to (get count of items in sourceList)
	set bufferList to {}
	set rangeList to {}
	set compiledList to {}
	-- get the positions in the list where the chapters end.
	repeat with x from 1 to itemCount
		set itemFound to false
		set itemX to item x of sourceList
		if x < itemCount then
			--this doesn't grab the last chapter
			repeat with y from (x + 1) to itemCount
				set itemY to item y of sourceList
				if itemY is itemX then set itemFound to true
			end repeat
		end if
		if itemFound is false then
			set end of bufferList to x
		end if
	end repeat
	--if no items are found
	if (get count of items in bufferList) is 0 then
		set end of rangeList to 1
		set end of rangeList to itemCount
		return rangeList
	end if
	--return bufferList --T'SHOOT

	--expand the list to start and end ranges
	set listCount to (get count of items in bufferList)
	repeat with x from 1 to listCount
		--get the items and prep the range "object"
		set thisItem to item x of bufferList
		set thisRange to {}
		if x = 1 then
			set end of thisRange to 1
			set end of thisRange to thisItem
		else if x > 1 then
			set prevItem to item (x - 1) of bufferList
			set end of thisRange to (prevItem + 1)
			set end of thisRange to thisItem
		end if
		set end of compiledList to thisRange
	end repeat
	return compiledList
end CreateNumberRanges

Shuffle List

Pseudo-random shuffling of a list containing anything.

on randomizeList(theList) -- ( list ) as list
	set lastItem to (count theList)
	set rmax to lastItem
	repeat with i from 1 to lastItem
		set r to random number from 1 to rmax
		set obj1 to item rmax of theList
		set obj2 to item r of theList
		set item rmax of theList to obj2
		set item r of theList to obj1
		set rmax to rmax - 1
	end repeat
	log theList
	return theList
end randomizeList