Parsing ranges: 1;2-6;10;12;20-25

Hi there.

I need some help on parsing a string with ranges like the one in the thread title.
I need a list of all the numers that the range-string specifies.
This is what I have so far:

set range to parseRange("1;1;1;3-6;5-8;12;20-25;21", ";", "-")
return range

on parseRange(range, singleSeparator, rangeSeparator)
	set rangeParts to splitText(singleSeparator, range)
	set listOfRanges to {}
	
	-- get single values out of ranges-pattern (1;2; etc.)
	repeat with theRange in (the items of rangeParts)
		if (the text of theRange) does not contain "-" then
			set listOfRanges to (listOfRanges & theRange)
		end if
	end repeat
	
	-- get range values out of range-pattern (2-4 or 5-25 etc.)
	repeat with theRange in the items in rangeParts
		if the text of theRange contains rangeSeparator then
			set subRanges to splitText(rangeSeparator, theRange)
			set numFrom to (the first item of subRanges)
			set numTo to (the last item of subRanges)
			-- loop from-to
			repeat with theNum from numFrom to numTo
				set listOfRanges to (listOfRanges & (theNum))
			end repeat
		end if
	end repeat
	
	-- I want to delete doubles here
	set testList to {}
	repeat with checkItem in (the items of listOfRanges)
		if checkItem is not in testList then
			set testList to (testList & checkItem)
		end if
	end repeat
	
	return testList
end parseRange

First I split all the single values separated by “;” and add them to the list.
Then I loop all the values specifying a range (x-y) and loop the numbers between x and y, adding them all to the list.

The part which starts with “set testList to {}” deletes all the double entries.
That all works. The problem is, that when I add the numbers between x and y, I add “real” numbers, instead of list items.
That probably doesn’t make sense, so here’s the return value of the script, with the range in the thread title as input:

{“1”, “12”, “3”, “6”, 4, 5, “5”, “8”, 6, 7, “20”, “25”, 21, 22, 23, 24}

You see, the “remove doubles” part of the script removes the 1’s at the beginning, because they’re list items.
But it won’t remove the double “21”, because it’s a number.

How can I set up the script so it can get all the doubles out?
Or even better, to add those numbers between x and y as list items, like all the other entries?

I hope you understand what I’m trying to say.

Thanks for any help!

Hi,

suggestion:


set theList to {}
set range to "1;1;1;3-6;5-8;12;20-25;21"
set {TID, text item delimiters} to {text item delimiters, ";"}
set listItems to text items of range
set text item delimiters to TID
repeat with i in listItems
	if i contains "-" then
		set {TID, text item delimiters} to {text item delimiters, "-"}
		repeat with j from (text item 1 of i as integer) to (text item 2 of i as integer)
			addListItem(theList, j as integer)
		end repeat
		set text item delimiters to TID
	else
		addListItem(theList, i as integer)
	end if
end repeat
theList

on addListItem(theList, l)
	if {l} is not in theList then set end of theList to l
end addListItem

Hi Stefan

Wow you really streamlined that. Works great. Thanks.
I added this to the addListItem function:

if {l} is not in theList and {l} is greater than 0 then set end of theList to l

Because I got 0 values in the list if the range was 2-5 for instance.

Have a nice day.

Gruess us Bärn :slight_smile: