Solved

# Build a Number Range

Posted on 2012-04-12
228 Views
Hi Experts,

I have been unable to do this and hoping somebody can help!  I have the following string of comma delimited numbers and that I need to breakdown and display as a range.

String: 1,2,3,4,6,7,8,9,10,11,12,15,19,21,22

Display as: 1-4, 6-12, 15, 19, 21-22

Any help would be greatly appreciated,

Eric
0
Question by:ehart12
• 2
• 2

LVL 52

Accepted Solution

_agx_ earned 500 total points
ID: 37839794
``````<!--- always sort first so the ranges make sense --->
<cfset numbers = listToArray("1,2,3,4,6,7,8,9,10,11,12,15,19,21,22")>
<cfset arraySort(numbers, "numeric")>

<cfset startAt = 1>
<cfset maxPosition = arrayLen(numbers)>
<cfset ranges = []>

<cfloop from="1" to="#maxPosition#" index="pos">
<!--- check difference between current and next number --->
<cfset offset = pos lt maxPosition ? 1 : 0>
<cfset gap = numbers[pos+offset] - numbers[pos]>

<!--- there was a jump in sequence OR this is the last element --->
<cfif gap gt 1 OR pos eq maxPosition>
<cfif startAt lt pos>
<!--- range --->
<cfset arrayAppend(ranges, numbers[startAt] &"-"& numbers[pos])>
<cfelse>
<!--- single value --->
<cfset arrayAppend(ranges, numbers[pos])>
</cfif>
<cfset startAt = pos + 1>
</cfif>
</cfloop>

List of Ranges: <cfdump var="#arrayToList(ranges, ',')#">
``````
0

Author Comment

ID: 37842531
Hi _agx_,

Once again, thank you the above solution!  What would be the best way to apply some type of numberformat so the actual format is as follows (three places with leading 0 to fill);

Is:  1-4, 6-12, 15, 19, 21-22

Desired: 001-004, 006-012, 015, 019, 021-022

Thank you,

Eric
0

LVL 52

Expert Comment

ID: 37843241
It's probably simplest to do it before adding the values to the array. Totally untested, but something along these lines

...
<cfif startAt lt pos>
<!--- range --->
<cfset fromValue = NumberFormat( numbers[startAt] , "000")>
<cfset toValue = NumberFormat( numbers[pos] , "000")>
<cfset arrayAppend(ranges, fromValue  &"-"& toValue )>
<cfelse>
<!--- single value --->
<cfset arrayAppend(ranges,  NumberFormat(numbers[pos], "000"))>
</cfif>
0

Author Closing Comment

ID: 37843249
Excellent, thank you!
0

## Featured Post

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …