Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Build a Number Range

Posted on 2012-04-12
4
Medium Priority
?
242 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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Loops Section Overview

963 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