[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 494
  • Last Modified:

Coldfusion - how to determine if number in ValueList() is within a specified range

How do I go about taking a valuelist such as (1, 5, 12, 18) and running a function to determine if any of the values in the list are within a specified range.  For example, if the range was from 2 to 6, I need an output that would that would state that the valuelist DOES have a value in that range, which is 5 in this example.

<cfset mylist= ValueList(myquery.ids,",")>
Now I need something like <cfif a value in mylist is between 2 and 6>Value exists</cfif>
2 Solutions
#listInRange("3,6,12",  4,  10)#   will return true

This assumes that if if the "from" or "to" range is blank, that there is no bound.  

#listInRange("3,6,12",  10, '')#     will return true if any number above 4

<cffunction name="listInRange" returnType="boolean">
  	 <cfargument name="theList">
  	 <cfargument name="fromRange">
  	 <cfargument name="toRange">
   <cfloop index="aVal" list="#arguments.theList#">
      <cfif (len(arguments.fromRange) eq 0 or aVal gte arguments.fromRange)
        and (len(arguments.toRange) eq 0 or aVal lte arguments.toRange)>
          <cfreturn true>
   <cfreturn false>

Open in new window

Andrew MaurerCommented:

<cfset fromval = 3 >
<cfset toval = 6 >
<cfset mylist = "2,56,6,78,9">
<cfset foundValue = false>
<cfloop from="#fromval#" to="#toval#" index="i">
	<cfif listfind(mylist,i)>
		<cfset foundValue = true>
<cfif foundvalue> TOLD YA I'D FIND ONE</cfif>

Open in new window

both code samples posted so far are good ones, but if you let us know your reason for this, we might come up with yet another example, which may be suited better to your purpose...

i.e. depending on the size of your list, and the range you are checking for, it may be more efficient to just run a QoQ on your original query to select IDs that fall within your range, instead of looping over list/range...

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Just to let you know gdemaria's answer is correct.
Andrew MaurerCommented:
Slick_moe, choose whatever fits your needs, they both are correct. Not sure where Nathans (the non-expert) comment is coming from...
Real simpleZadoc I was one of the past experts top of the charts here and have not been back for a while.  He asked for a FUNCTION.  I understand your answer will work but it is not a FUNCTION.  I have been doing ColdFusion for a VERY long time.  If you guys had NOT answered correctly I would have posted a answer.  Sorry if I offended you but I do not like questions hanging around that have a legitimate answer that is correct for the question asked.  So instead of providing a new answer I simply made sure to tell the person asking the question to please accept it.  If the answer was incomplete and you gave a more complete answer I would have explained that.  I appreciated you trying.  If you want to check and see http://www.linkedin.com/in/nathanstanford here is my work record to show I am an expert in ColdFusion.
Andrew MaurerCommented:
So if he asked for a function, Gdemaria's answer is a function.. You really think he needs your help determining which one is a function?

Sorry I offended you. You are an expert.

I don't feel your comment was appropriate and it was extremely vague.
Andrew MaurerCommented:
Also Nathans... you spelt "Verision" wrong on your Linkedin profile.

Its     Verizon
slick_moeAuthor Commented:
I just have to say that gdemaria's answer is a "function," which is what I asked for.  I wasn't after a "function" per se, only code that would give the functionality I needed.  It's my fault for not being more clear.  Being more of a novice, Zadoc's answer worked perfectly for my needs and so get's the most points.  Some points also to gdemaria for a great answer as well.  Thanks to all!

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now