Skip to main content
Participant
December 5, 2011
Question

find Index for max value of array

  • December 5, 2011
  • 3 replies
  • 3019 views

I can use arrayMax to get the max value for a small array, but I need to get which index the max value is located. That will determine which page the user goes to next. Here is my code:

array:

<cfset arrAverages[1]="#qryFairness.fairness#"/>

<cfset arrAverages[2]="#qryHonesty.honesty#"/>

<cfset arrAverages[3]="#qryCompassion.compassion#"/>

<cfset arrAverages[4]="#qrySelfControl.SelfControl#"/>

<cfset arrAverages[5]="#qryMoralConcern.MoralConcern#"/>

These values are gathered from querying values stored in a table.

I can get the max value in the array easily by:

<cfdump var="#arrayMax(arrAverages)#">

What I can't figure out is how to get the row in this array where that max value is located. It seems like it would be so simple, but nothing I have tried has worked.

Thanks,

Richard

This topic has been closed for replies.

3 replies

BKBK
Community Expert
Community Expert
December 7, 2011

rking1966 wrote:

I can use arrayMax to get the max value for a small array, but I need to get which index the max value is located. That will determine which page the user goes to next. Here is my code:

array:

<cfset arrAverages[1]="#qryFairness.fairness#"/>

<cfset arrAverages[2]="#qryHonesty.honesty#"/>

<cfset arrAverages[3]="#qryCompassion.compassion#"/>

<cfset arrAverages[4]="#qrySelfControl.SelfControl#"/>

<cfset arrAverages[5]="#qryMoralConcern.MoralConcern#"/>

These values are gathered from querying values stored in a table.

I can get the max value in the array easily by:

<cfdump var="#arrayMax(arrAverages)#">

What I can't figure out is how to get the row in this array where that max value is located. It seems like it would be so simple, but nothing I have tried has worked.


You can find it in one line of code! Here is an example:

<cfset testArr[1]=-183>

<cfset testArr[2]=79>

<cfset testArr[3]=6>

Max.:  <cfoutput>#arraymax(testArr)#</cfoutput><br>

Index of max.: <cfoutput>#arrayfindNocase(testArr, arraymax(testArr))#</cfoutput>

Inspiring
December 7, 2011

arrayfindNocase

Unfortunately they are using CF8.

Inspiring
December 7, 2011

You can leverage the fact that a CF array is a Java Vector, so you oculd use indexOf().

http://docs.oracle.com/javase/6/docs/api/java/util/Vector.html

This is not strictly supported, but realistically, CF is not going to reimplement arrays as anything else in the foreseeable future.

--

Adam

rking1966Author
Participant
December 6, 2011

Dan, you have brought up a very interesting point. If arrayMax would give one of the max values if a tie occurs, that would be fine for this project as well. But I'll have to test that out. I did play with the list thing, but I couldn't get it to work that way either. I'm also going to see now if I can do this in the db and just use the max value I get there on my page.

Inspiring
December 6, 2011

If arrayMax would

give one of the max values if a tie occurs

ArrayMax always returns a single value. The position of the maximum value within an array is a different story.

What is your real goal here?

rking1966Author
Participant
December 6, 2011

So here's the whole story/goal:

I have a survey (one of several for this project) in which the questions are grouped and scored (just on backend - users never know). The highest average group determines which page the user is sent to next. So I'm submitting the data to the db, querying the averages for each trait, now I need to get the max average to determine which page they go to next. There are 5 groups/averages. I can put these in an array or a struct but haven't yet been able to easily pull the highest GROUP out (which would be the POSITION in the array). I can pull the highest average out, but I need the position (or group) where that max occurs. It seems like it would be such an easy thing, but I'm stuck. I know I risk overlapping entries (or querying the wrong entry) by doing this after the data is submitted into the db, but I think that risk is very small and I don't know how to better do these calculations until data is in a table.

As you can see, I'm not an expert user. I'm running CF8....sounds like I could use "arrayFind" if using CF9 and all would work, but that's not an option at this time.

Inspiring
December 5, 2011

For CF9 and above you can use ArrayFind/FindNoCase. Otherwise just loop through the array, and cfbreak when you find the matching value.

Inspiring
December 5, 2011

Or convert it to a list and use ListFind.  But what if there are two matching records?