Solved

Build a Number Range

Posted on 2012-04-12
4
228 Views
Last Modified: 2012-04-13
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
Comment
Question by:ehart12
  • 2
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
_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, ',')#">

Open in new window

0
 

Author Comment

by:ehart12
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

by:_agx_
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

by:ehart12
ID: 37843249
Excellent, thank you!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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: …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now