Solved

How to search a list in coldfusion for a value that is less than a predifined value

Posted on 2003-02-20
Medium Priority
190 Views

I have a situation where I have a list with 50 numbers of values which increment by 10000. What I want to do is find a value in that list that is less than a predifined variable. FOR EXAMPLE:
VARIABLE X = 128,000
I want to search that list so that it will find the max value that is less than \$128,000. Which in the case of the list I have will be \$120,000.

Anybody have any suggestions on how to do this?

0
Question by:dmatthews
• 3

LVL 1

Accepted Solution

orangachang earned 800 total points
ID: 7989039
This should do it...

<cfoutput>
<cfset x = 128000>
<cfset y = 0>
<cfloop index="i" from="1" to="#ListLen(MyList)#">
<cfif ListGetAt(MyList, i) lte x>
<cfset y = i>
</cfif>
</cfloop>
#ListGetAt(MyList, y, ",")#
</cfoutput>

Bob
0

Author Comment

ID: 7989204
BUt will that get the max value. IE. There is a list with 10 value. 10,000 -100,000. The value for x is 85,000. Going through the list the first value of 10,000 will work that that is what y will be set to when i really want y to be 80,000.
0

Author Comment

ID: 7989315
BUt will that get the max value. IE. There is a list with 10 value. 10,000 -100,000. The value for x is 85,000. Going through the list the first value of 10,000 will work that that is what y will be set to when i really want y to be 80,000.
0

LVL 15

Expert Comment

ID: 7989667
orangachang's code will work, but here is a slight improvement for you that doesn't loop through every record if it doesn't have to, and also makes sure the list is sorted properly first...

<CFSET MyList = "20000,30000,10000,40000,50000">
<CFSET MyListArray = listtoarray(Mylist)>
<CFSET sortlist = arraysort(MyListArray,"numeric")>
<CFSET MyList = arraytolist(MyListArray)>

<cfset x = 28000>
<cfset y = 0>

<cfloop from="1" to="#ListLen(MyList)#" index="i">
<cfif ListGetAt(MyList,i) gt x>
<cfset y = i-1>
<CFBREAK>
</cfif>
</cfloop>

<CFIF y lte 0>
no value lower found
<CFELSE>
<cfoutput>#ListGetAt(MyList,y)#</cfoutput>
</cfif>
0

Author Comment

ID: 7991085
YOur code works however I found that if I tried to continuously update x inside the loop it wouldnt finish going through the values. I had the following

<cfoutput>
<cfset x = 128000>
<cfset y = 0>
<cfloop index="i" from="1" to="#ListLen(MyList)#">
<cfif ListGetAt(MyList, i) lte x>
<cfset x = i>
</cfif>
</cfloop>
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…
Suggested Courses
Course of the Month4 days, 5 hours left to enroll