?
Solved

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

Posted on 2003-02-20
5
Medium Priority
?
188 Views
Last Modified: 2013-12-24

 
  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
Comment
Question by:dmatthews
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 1

Accepted Solution

by:
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

by:dmatthews
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

by:dmatthews
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

by:danrosenthal
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

by:dmatthews
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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
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 make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question