On 10/2/07, Andy Matthews <[EMAIL PROTECTED]> wrote:
> Say that I have a list of allowed nmbers:
> 32,48,64,72,144,160,200,288,320,400,512,576,640,720,800
>
> If I give the user the option of selecting a number, and it happens to not be 
> in this list, how might I go about automagically selecting the next lowest 
> number? One exception being if the user selects a number lower than 32, in 
> which case the code should return 32.
>
> Examples:
> User selects 100, the code would return 72.
> User selects 480, the code would return 400.
> User selects 25, the code would return 32.
>
> Currently the numbers are stored in a simple list, but I have control over 
> that list and they can be in whatever format makes the most sense. One caveat 
> is that they need to stay in code (so no database interaction).

dunno if this is particularly elegant or not... :)

<cfset myList = "32,48,64,72,144,160,200,288,320,400,512,576,640,720,800" />

<cfset userSelect = 25 />       <!--- user's selected number here --->
<cfset userAnswer = "" />       <!--- the value to be returned here --->        

<cfif listFind(myList, userSelect) GT 0>
        <cfset userAnswer = userSelect />
<cfelse>
        <cfset myNewList = listSort(myList, 'numeric') />

        <cfloop list="#myNewList#" index="idx">
                <cfif userSelect LTE idx>
                        <cfset userAnswer = idx />
                        <cfbreak />
                </cfif>
        </cfloop>
        
        <cfif listFind(myNewList, userAnswer) NEQ 1>
                <cfset userAnswer = listGetAt(myNewList, listFind(myNewList, 
userAnswer)-1) />
        </cfif>
</cfif>

<cfoutput>#userAnswer#</cfoutput>

-- 
Charlie Griefer

================================================
"...All the world shall be your enemy, Prince with a Thousand Enemies,
and whenever they catch you, they will kill you. But first they must catch
you, digger, listener, runner, prince with a swift warning.
Be cunning and full of tricks and your people shall never be destroyed."

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Create robust enterprise, web RIAs.
Upgrade to ColdFusion 8 and integrate with Adobe Flex
http://www.adobe.com/products/coldfusion/flex2/?sdid=RVJP

Archive: 
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:290037
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4

Reply via email to