Solved

Build a Number Range

Posted on 2012-04-12
4
235 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

679 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