Skip to main content
Inspiring
July 29, 2009
Answered

Getting high and low number in a string

  • July 29, 2009
  • 3 replies
  • 1411 views

Hi

I have the following

<cfset thelist = "1,5,8,11,22,76,6,143,147">

How do i get the highest and lowest number out of that string?

Thanking you in advance for your help!

Regards

Delon

This topic has been closed for replies.
Correct answer -__cfSearching__-

Use the very well named ListToArray() function .. and definitely bookmark the online documentation ;-)  The functions by category view is invaluable.

http://livedocs.adobe.com/coldfusion/8/functions-pt0_13.html

3 replies

Inspiring
July 30, 2009

If you take this approach (which is a fine one), don't forget to build error-tolerance into the process, by means of the <cftry> and <cfcatch> tags.

It is reasonable to assume that, from time to time, this routine may be handed un-processable inputs, which will result in various kinds of exceptions being "thrown."  Your application, however, should not "fall flat on its face" when this occurs.  Instead, it should "catch" them and respond to the situation in some intelligent way.

This is, by the way, a perfectly-appropriate validation technique for cases like this.  You know that "ka-ka might occur," whether it is highly-likely or not-at-all.  Instead of spending gobs of computing-cycles checking every single input against "what might go wrong," you simply post someone in the outfield with a big glove.  He might spend weeks with nothing to do, but each and every time "ka-ka occurs," he will catch the ball.

BKBK
Community Expert
Community Expert
July 30, 2009
ka-ka occurs

Quite philosophical for French readers.

BKBK
Community Expert
Community Expert
July 29, 2009

<cfset sortedList = listSort(thelist, "numeric", "Asc")>

min: <cfoutput>#listFirst(sortedList)#</cfoutput><br>
max: <cfoutput>#listLast(sortedList)#</cfoutput>

Inspiring
July 29, 2009

One approach might be to convert the list to an array and use arrayMax/arrayMin.  Assuming the list contains valid numbers, of course.

Inspiring
July 29, 2009

Hi

Thanks for the reply.

Ok, so my next question would be... how do i convert that list to an array?

Excuse my ignorance. Im quite new to it!

-__cfSearching__-Correct answer
Inspiring
July 29, 2009

Use the very well named ListToArray() function .. and definitely bookmark the online documentation ;-)  The functions by category view is invaluable.

http://livedocs.adobe.com/coldfusion/8/functions-pt0_13.html