First, your result list is incorrect.  It should start with "1-6,10-14"
rather than "1-14".  Here's some code that'll do what you want (using your
list):

<!--- supplied list --->
<cfset var1 = "1,2,3,4,5,6,10,11,12,13,14,20,25,30,31,32,33,50" />

<!--- compute the 'ranged' list --->
<cfset last = "" />
<cfset array = listToArray(var1) />
<cfset result = "" />
<cfset startedRange = false />
<cfloop from="1" to="#arrayLen(array)#" index="i">
<cfif last EQ array[i] - 1>
<cfset startedRange = true />
<cfelse>
<cfif startedRange>
<cfset result = listAppend(result, array[i - 1],
"-") />
<cfset startedRange = false />
</cfif>
</cfif>
<cfif NOT startedRange>
<cfset result = listAppend(result, array[i]) />
</cfif>
<cfset last = array[i] />
</cfloop>

<!--- output --->
<cfoutput>
<pre>
initial : #var1#
desired : 1-6,10-14,20,25,30-33,50
computed: #result#
</pre>
</cfoutput>

> -----Original Message-----
> From: Nathan R. Jessop [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, May 19, 2004 10:57 AM
> To: CF-Talk
> Subject: RE: **UPDATE** Arrange sequential selections into
> ranges how to???
>
> Maybe I've made it more difficult than it has to be.
>
> Ignore the <select> stuff.
>
>
> Let's say I have a variable being passed to my action page
> that contains the following (this can vary):
>
>
> var1 = "1,2,3,4,5,6,10,11,12,13,14,20,25,30,31,32,33,50"
>
>
> How can I arrange the above variable into a range.
> Meaning...how can I take any input from the variable and
> basically create a new varaible with the following:
>
>
> var2 = "1-14,20,25,30-33,50"
>
> Notice that if the numbers run consequtively they are
> "grouped" starting with first then last separated by a hyphen.
>
> Is this possible? I know what I want to do but its how to
> implement it.
>
>
>
> >> I need to pass it like  1-16,23-25,30,33,40-42,46-48 to the server.
>
> On Wed, 19 May 2004 17:41:35 +0200, Pascal Peters wrote:
>
> >Rename your select "filelistselect",
> >create a hidden field named "filelist"
> >and call this JS function in the onsubmit of the form:
> >
> >function makeList(thisform){
> > var opts = thisform.filelistselect.options,
> >     val="",
> >     i=0;
> > for(;i<opts.length;i++){
> > if(opts[i].selected){
> > if(i==0 || !opts[i-1].selected){
> > val += ((val.length>0)?",":"") +
> >opts[i].value;
> > }
> > else if(i==opts.length-1 ||
> >!opts[i+1].selected){
> > val += "-" + opts[i].value;
> > }
> > }
> > }
> > thisform.filelist.value = val;
> > thisform.filelistselect.disabled = true;
> >}
> >
> ><form ... > > >
> >Pascal
> >
> >
> >>
> >>
> >> I need to pass it like  1-16,23-25,30,33,40-42,46-48 to the server.
> >>
> >> >> <select name="filelist" size="15" multiple
> class="dirlinks">
> >> >>
> >> >> <cfloop INDEX="i" FROM="1"
> TO="#ArrayLen(arPages)#">
> >> >>
> >> >> <cfset sVal = Trim(ListFirst(arPages[i], ">"))>
> >> >>
> >> >> <cfset sDisp = Trim(ListLast(arPages[i], ">"))>
> >> >>
> >> >> <cfset sValNoComma = NumberFormat(sVal,
> "9999999999")>
> >> >>
> >> >> <cfset sVal = NumberFormat(sVal)>
> >> >>
> >> >> <option value="#sValNoComma#">Seq:
> >> >> #sValNoComma#&nbsp;&nbsp;Folio: #sDisp##spacer1#</option>
> >> >>
> >> >> </cfloop>
> >> >>
> >> >> </select>
> >
> >
> >
>
>
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings]

Reply via email to