Link to home
Start Free TrialLog in
Avatar of ehart12
ehart12Flag for United States of America

asked on

Build a Number Range

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
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ehart12

ASKER

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
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>
Avatar of ehart12

ASKER

Excellent, thank you!