Copy link to clipboard
Copied
I have an app that passes me a series of numbers (nine actually) that I need to total and average. The only problem is that I need to drop the two lowest numbers before doing the math.
So for example, let's say I get these numbers:
5
4
5
2
4
1
2
4
5
I need to drop the 1 and one of the 2s, then total them and get the average number.
I was thinking about converting them to an array, but I still can't figure out how to drop the lowest two.
Any ideas?
Copy link to clipboard
Copied
listSort()
listRest()
listRest()
--
Adam
Copy link to clipboard
Copied
Have you tried looking in the documentation under the conveniently-titled "array functions" section?
1) Get them into an array (how depends on what format they're in to start with)
2) ArraySort()
3) ArrayDeleteAt(1)
4) ArrayDeleteAt(1)
5) ArraySum()
6) ArrayAvg()
It's like some kind of Function Bingo, I reckon you've got most of the available functions used there.
Copy link to clipboard
Copied
Owainnorth wrote:
Have you tried looking in the documentation under the conveniently-titled "array functions" section?
I was actually looking at that when I was typing the post, but doing two ArrayDeleteAts seemed a little clunky and I wondered if there was an easier/better way.
BUT, I guess if it works it works!
Copy link to clipboard
Copied
Ok, what on earth am I doing wrong?
I came up with this:
<cfset subtotal = ArrayNew(1)>
<cfset subtotal[1] = cpi_score>
<cfset subtotal[2] = inno_score>
<cfset subtotal[3] = pc_score>
<cfset subtotal[4] = int_score>
<cfset subtotal[5] = equ_score>
<cfset subtotal[6] = ls_score>
<cfset subtotal[7] = drer_score>
<cfset subtotal[8] = css_score>
<cfset subtotal[9] = iis_score>
<cfset subtotal = ArraySort(subtotal,"numeric","asc")>
<cfset subtotal = ArrayDeleteAt(subtotal,1)>
<cfset subtotal = ArrayDeleteAt(subtotal,1)>
<cfset total_score = ArraySum(subtotal) * 2.857>
But it gives me this:
Object of type class java.lang.Boolean cannot be used as an array
115 : <cfset subtotal[9] = iis_score>
116 : <cfset subtotal = ArraySort(subtotal,"numeric","asc")>
117 : <cfset subtotal = ArrayDeleteAt(subtotal,2)>
118 : <cfset subtotal = ArrayDeleteAt(subtotal,1)>
119 : <cfset total_score = ArraySum(subtotal) * 2.857>
I don't get what I'm boogering up.
Copy link to clipboard
Copied
Object of type class java.lang.Boolean cannot be used as an array
117 : <cfset subtotal = ArrayDeleteAt(subtotal,2)>
I don't get what I'm boogering up.
Some questions:
* what does the error say? Obviously it's clear what it says, but think through what's happened to make it say that.
* what is the value of subtotal just before that line of code? Why?
* read the docs for arraySort() and arrayDeleteAt(). What do those functions return?
arraySort()
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f14.html
arrayDeleteAt()
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f1e.html
The first port of call when troubleshooting an error should be to assess what the error is saying, then check the values that contribute to the line of code that's erroring, and always read the docs for the functionality you're using. This'll get you through an awful lot of "mysterious" code errors.
--
Adam
Copy link to clipboard
Copied
<cfset subtotal = ArrayNew(1)> <cfset subtotal[1] = cpi_score> <cfset subtotal[2] = inno_score> <cfset subtotal[3] = pc_score> <cfset subtotal[4] = int_score> <cfset subtotal[5] = equ_score> <cfset subtotal[6] = ls_score> <cfset subtotal[7] = drer_score> <cfset subtotal[8] = css_score> <cfset subtotal[9] = iis_score>
If you're using CF8 onwards, you could streamline this into one statement:
<cfset subtotal = [cpi_score, inno_score,...,iis_score]>
Also... it's an array of scores... it's not really a "subtotal" so the variable perhaps could stand renaming to something close to what it is: scoreArray or aScores or something. Keeping your variable names meaningful is a great benefit to people who end up maintaining your code.
--
Adam
Copy link to clipboard
Copied
Hopefully you are following Adam's advice and learning some good troubleshooting skills. Adam is a "teach a man to fish" kinda guy.
But just in case you are under the wire or ready to spit nails. I will offer this.
<cfset subtotal = ArraySort(subtotal,"numeric","asc")>
ArraySort() does not return what you think it returns. (Neither does ArrayDeleteAt())
<cfset subtotal = ArrayDeleteAt(subtotal,1)>
Which means that subtotal here does not contain what you think it contains.
Copy link to clipboard
Copied
Ok, I've been fiddling and researching and I think I have it figured out.
I'm pretty sure I knew what everything contained, because I was <cfdump>ing the array to see what my changes were doing. I think I just had a syntax problem, which I discovered with the help of Ray Camden's blog.
<cfset subtotal = ArrayDeleteAt(subtotal,1)>
Should have been:
<cfset ArrayDeleteAt(subtotal,1)>
That got rid of the error message. I had to do that (remove the "subtotal =" part) for both ArrayDeleteAts and the ArraySort.
Here's what I ended up with:
<cfset subtotal = ArrayNew(1)>
<cfset subtotal[1] = cpi_score>
<cfset subtotal[2] = inno_score>
<cfset subtotal[3] = pc_score>
<cfset subtotal[4] = int_score>
<cfset subtotal[5] = equ_score>
<cfset subtotal[6] = ls_score>
<cfset subtotal[7] = drer_score>
<cfset subtotal[8] = css_score>
<cfset subtotal[9] = iis_score>
<cfset ArraySort(subtotal,"numeric","asc")>
<cfset ArrayDeleteAt(subtotal,1)>
<cfset ArrayDeleteAt(subtotal,1)>
<cfset total_score = ArraySum(subtotal) * 2.857>
Which works! I had to add an ArrayClear (inside a <cfif IsArray> check) beforehand to clear the Array from the previous loop, but it seems to work really well now.
But I'll definitely be renaming that variable as per Adam's advice.
Thanks everyone.
Copy link to clipboard
Copied
For those that find this thread in the future, and for your benefit, I'llpoint out that this wasn't really a syntax problem. Technically, the syntax was fine. It was an expectation problem.
You were expecting ArraySort() and ArrayDeleteAt() to return the modified arrays, when infact, they both return booleans.
Copy link to clipboard
Copied
I wonder then how I got it to work at all *headscratch*
Copy link to clipboard
Copied
There's a difference between a syntax problem and a logic problem. You had an error in your logic in that you were erroneously overwriting your array with the return value of the array function. It wasn't a syntax issue. If you had a syntax issue, you would have got a compile error, not a runtime error.
Jason's just pointing out the misuse of your terminology, not that there was anything wrong with your solution.
--
Adam
Copy link to clipboard
Copied
Jason's just pointing out the misuse of your terminology, not that there was anything wrong with your solution.
Thanks. I still wanted to understand the original problem though. I think I do now based on your last post.