Solved

Build a Number Range

Posted on 2012-04-12
4
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

734 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