|
Mailing Lists
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
**UPDATE** Arrange sequential selections into ranges how to???
Maybe I've made it more difficult than it has to be.Nathan R. Jessop 05/19/04 01:58 P Here's my 3 minute attempt:Steve Nelson 05/19/04 02:07 P Sorry about that... Here's my 3 minute attempt. I haven't tested it. At aSteve Nelson 05/19/04 02:29 P Steve,Barney Boisvert 05/19/04 02:40 P <cfset var1 = "1,2,3,4,5,6,10,11,12,13,14,20,25,30,31,32,33,50">Steve Nelson 05/19/04 02:53 P Still not quite right:Barney Boisvert 05/19/04 04:48 P First, your result list is incorrect. It should start with "1-6,10-14"Barney Boisvert 05/19/04 02:09 P Hmm...Nathan R. Jessop 05/20/04 09:37 A here is your answer:Ian Sheridan 05/20/04 10:10 A OOPSIan Sheridan 05/20/04 10:22 A Where do I place this?Nathan R. Jessop 05/20/04 10:49 A heh ok sorry here is the full script:Ian Sheridan 05/20/04 10:56 A I would use a case construct. Or even use the MOD function with an array toAndy Ousterhout 05/20/04 10:14 A I would use a case construct. Or even use the MOD function with an array toAndy Ousterhout 05/20/04 10:54 A Nathan,Pascal Peters 05/20/04 08:52 P 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. ----- Excess quoted text cut - see Original Post for more ----- Here's my 3 minute attempt: _____ Sent: Wednesday, May 19, 2004 1:57 PM 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. ----- Excess quoted text cut - see Original Post for more ----- _____ Sorry about that... Here's my 3 minute attempt. I haven't tested it. At a second glance I think it screws up the first group. Steve Nelson <cfset var1 = "1,2,3,4,5,6,10,11,12,13,14,20,25,30,31,32,33,50"> <cfset var2=""> <cfset lastitem=listfirst(var1)-1> <cfset groupstart=lastitem> <cfloop list="#var1#" index="item"> <cfif item gt lastitem+1> <cfset newgroup="#groupstart#-#lastitem#"> <cfset var2=listappend(var2,newgroup)> <cfset groupstart=item> </cfif> </cfloop> _____ Sent: Wednesday, May 19, 2004 2:04 PM To: CF-Talk Subject: RE: **UPDATE** Arrange sequential selections into ranges how to??? Here's my 3 minute attempt: _____ Sent: Wednesday, May 19, 2004 1:57 PM 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. ----- Excess quoted text cut - see Original Post for more ----- _____ _____ Steve, Here's the output I get from your code: initial : 1,2,3,4,5,6,10,11,12,13,14,20,25,30,31,32,33,50 desired : 1-6,10-14,20,25,30-33,50 computed: 0-0,2-0,3-0,4-0,5-0,6-0,10-0,11-0,12-0,13-0,14-0,20-0,25-0,30-0,31-0,32-0,33 -0 Cheers, barneyb ----- Excess quoted text cut - see Original Post for more ----- <cfset var1 = "1,2,3,4,5,6,10,11,12,13,14,20,25,30,31,32,33,50"> <cfset var2=""> <cfset lastitem=listfirst(var1)> <cfset groupstart=lastitem> <cfloop list="#var1#" index="item"> <cfif item gt lastitem+1> <cfif item is listlast(var1)> <cfset newgroup=item> <cfelse> <cfset newgroup="#groupstart#-#lastitem#"> </cfif> <cfset var2=listappend(var2,newgroup)> <cfset groupstart=item> </cfif> <cfset lastitem=item> </cfloop> <cfoutput>#var2#</cfoutput> I left out the lastitem=item Steve _____ Sent: Wednesday, May 19, 2004 2:37 PM To: CF-Talk Subject: RE: **UPDATE** Arrange sequential selections into ranges how to??? Steve, Here's the output I get from your code: initial : 1,2,3,4,5,6,10,11,12,13,14,20,25,30,31,32,33,50 desired : 1-6,10-14,20,25,30-33,50 computed: 0-0,2-0,3-0,4-0,5-0,6-0,10-0,11-0,12-0,13-0,14-0,20-0,25-0,30-0,31-0,32-0,33 -0 Cheers, barneyb ----- Excess quoted text cut - see Original Post for more ----- _____ Still not quite right: initial : 1,2,3,4,5,6,10,11,12,13,14,20,25,30,31,32,33,50 desired : 1-6,10-14,20,25,30-33,50 computed: 1-6,10-14,20-20,25-25,50 ^^ ^^ ^^ The code in my initial post works correctly, and is reposted (with slight optimizations) below: <!--- 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 var2 = "" /> <cfset startedRange = false /> <cfloop list="#var1#" index="curr"> <cfif last EQ curr - 1> <cfset startedRange = true /> <cfelseif startedRange> <cfset var2 = var2 & "-" & last /> <cfset startedRange = false /> </cfif> <cfif NOT startedRange> <cfset var2 = listAppend(var2, curr) /> </cfif> <cfset last = curr /> </cfloop> Cheers, barneyb ----- Excess quoted text cut - see Original Post for more ----- 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> ----- Excess quoted text cut - see Original Post for more ----- Hmm... If my var1 is equal to... <cfset var1 = "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130"> I get this for the output: 1 It should be 1-130 Still not quite right: initial : 1,2,3,4,5,6,10,11,12,13,14,20,25,30,31,32,33,50 desired : 1-6,10-14,20,25,30-33,50 computed: 1-6,10-14,20-20,25-25,50 ^^ ^^ ^^ The code in my initial post works correctly, and is reposted (with slight optimizations) below: <cfset var1 = "1,2,3,4,5,6,10,11,12,13,14,20,25,30,31,32,33,50" /> <cfset last = "" /> <cfset var2 = "" /> <cfset startedRange = false /> <cfloop list="#var1#" index="curr"> <cfif last EQ curr - 1> <cfset startedRange = true /> <cfelseif startedRange> <cfset var2 = var2 & "-" & last /> <cfset startedRange = false /> </cfif> <cfif NOT startedRange> <cfset var2 = listAppend(var2, curr) /> </cfif> <cfset last = curr /> </cfloop> Cheers, barneyb ----- Excess quoted text cut - see Original Post for more ----- here is your answer: <cfscript> var2 = ""; n = listlen(var1); for (i=1;i LTE n;i=i+1) { curr = listgetat(var1,i); if (i LT n) { next = listgetat(var1,i + 1); } else { next = ""; } if (i GT 1) { last = listgetat(var1,i - 1); } else { last = ""; } if (curr + 1 NEQ next) { var2 = listappend(var2,i,"-"); } else if (curr - 1 NEQ last) { var2 = listappend(var2,i); } } </cfscript> you had to look forward and back. On May 20, 2004, at 9:36 AM, Nathan R. Jessop wrote: ----- Excess quoted text cut - see Original Post for more ----- OOPS here is is fixed <cfscript> var2 = ""; n = listlen(var1); for (i=1;i LTE n;i=i+1) { curr = listgetat(var1,i); if (i LT n) { next = listgetat(var1,i + 1); } else { next = ""; } if (i GT 1) { last = listgetat(var1,i - 1); } else { last = ""; } if (curr - 1 NEQ last) { var2 = listappend(var2,listgetat(var1,i)); } else if (curr + 1 NEQ next) { var2 = listappend(var2,listgetat(var1,i),"-"); } } </cfscript> On May 20, 2004, at 10:07 AM, Ian Sheridan wrote: ----- Excess quoted text cut - see Original Post for more ----- Where do I place this? On Thu, 20 May 2004 10:19:11 -0400, Ian Sheridan wrote: ----- Excess quoted text cut - see Original Post for more ----- heh ok sorry here is the full script: <cfscript> var1 = "1,2,3,4,5,6,10,11,12,13,14,20,25,30,31,32,33,50"; var2 = ""; n = listlen(var1); for (i=1;i LTE n;i=i+1) { curr = listgetat(var1,i); if (i LT n) { next = listgetat(var1,i + 1); } else { next = ""; } if (i GT 1) { last = listgetat(var1,i - 1); } else { last = ""; } if (curr - 1 NEQ last) { var2 = listappend(var2,listgetat(var1,i)); } else if (curr + 1 NEQ next) { var2 = listappend(var2,listgetat(var1,i),"-"); } } </cfscript> <!--- output ---> <cfoutput> <pre> initial : #var1# desired : 1-6,10-14,20,25,30-33,50 computed: #var2# </pre> </cfoutput> On May 20, 2004, at 10:48 AM, Nathan R. Jessop wrote: > Where do I place this? I would use a case construct. Or even use the MOD function with an array to load the values or count of values into an array: // NOTE: THIS IS OFF OF THE TOP OF A VERY EMPTY HEAD SO FORGIVE SYNTAX ERRORS RangeCount = 6; //Create ranges of 1-6, 7-12, etc NumRanges = 6; // Last range will be 36 and larger Rangess = arrayNew(NumRanges); // Array containing whatever you are looking for //Loop through list to put into ranges for (i;1;ListCount(var1);i=i+1) { //This statement will depend on what you are capturing in the ranges //This should count number of items in each range Ranges(Min(NumRanges, i mod rangeCount)) = Groups(Min(NumRanges, i mod RangeCount)) + 1 } Regards, Andy Hmm... If my var1 is equal to... <cfset var1 = "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130"> I get this for the output: 1 It should be 1-130 Still not quite right: initial : 1,2,3,4,5,6,10,11,12,13,14,20,25,30,31,32,33,50 desired : 1-6,10-14,20,25,30-33,50 computed: 1-6,10-14,20-20,25-25,50 ^^ ^^ ^^ The code in my initial post works correctly, and is reposted (with slight optimizations) below: <cfset var1 = "1,2,3,4,5,6,10,11,12,13,14,20,25,30,31,32,33,50" /> <cfset last = "" /> <cfset var2 = "" /> <cfset startedRange = false /> <cfloop list="#var1#" index="curr"> <cfif last EQ curr - 1> <cfset startedRange = true /> <cfelseif startedRange> <cfset var2 = var2 & "-" & last /> <cfset startedRange = false /> </cfif> <cfif NOT startedRange> <cfset var2 = listAppend(var2, curr) /> </cfif> <cfset last = curr /> </cfloop> Cheers, barneyb > ranges how to??? > > <cfset var1 = "1,2,3,4,5,6,10,11,12,13,14,20,25,30,31,32,33,50"> > > > > <cfset var2=""> > > <cfset lastitem=listfirst(var1)> > > <cfset groupstart=lastitem> > > <cfloop list="#var1#" index="item"> > > <cfif item gt lastitem+1> > > <cfif item is listlast(var1)> > > <cfset newgroup=item> > > <cfelse> > > <cfset > newgroup="#groupstart#-#lastitem#"> > > </cfif> > > <cfset var2=listappend(var2,newgroup)> > > <cfset groupstart=item> > > </cfif> > > <cfset lastitem=item> > > </cfloop> > > > > <cfoutput>#var2#</cfoutput> > > > > I left out the lastitem=item > > > > Steve I would use a case construct. Or even use the MOD function with an array to load the values or count of values into an array: // NOTE: THIS IS OFF OF THE TOP OF A VERY EMPTY HEAD SO FORGIVE SYNTAX ERRORS RangeCount = 6; //Create ranges of 1-6, 7-12, etc NumRanges = 6; // Last range will be 36 and larger Rangess = arrayNew(NumRanges); // Array containing whatever you are looking for //Loop through list to put into ranges for (i;1;ListCount(var1);i=i+1) { //This statement will depend on what you are capturing in the ranges //This should count number of items in each range Ranges(Min(NumRanges, i mod rangeCount)) = Groups(Min(NumRanges, i mod RangeCount)) + 1 } Regards, Andy Nathan, I don't know if you have a correct server side solution yet. Since it's a holiday here in Belgium (so I have a little time), I took a shot at it. I didn't start from any of the other solutions posted, but wrote my own: <cfscript> function ListToRanges(list){ var a = ListToArray(list); var out = ""; var i = 1; var max = ArrayLen(a); for(;i LE max;i=i+1){ if(i IS 1 OR a[i-1] LT a[i] - 1){ out = ListAppend(out,a[i]); } else if(i IS max OR a[i+1] GT a[i] + 1){ out = ListAppend(out,a[i],"-"); } } return out; } </cfscript> <cfoutput>#ListToRanges("1,2,3,4,5,7,9,10,11,12")#</cfoutput> Pascal ----- Excess quoted text cut - see Original Post for more -----
|
February 08, 2012
|
Latest Fusion Authority Articles
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||