Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Dropping smallest numbers from list

Contributor ,
Sep 06, 2011 Sep 06, 2011

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?

1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 06, 2011 Sep 06, 2011

listSort()

listRest()

listRest()

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 06, 2011 Sep 06, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 06, 2011 Sep 06, 2011

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 06, 2011 Sep 06, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 06, 2011 Sep 06, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 06, 2011 Sep 06, 2011
<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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Sep 06, 2011 Sep 06, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 06, 2011 Sep 06, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Sep 06, 2011 Sep 06, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 07, 2011 Sep 07, 2011

I wonder then how I got it to work at all *headscratch*

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 07, 2011 Sep 07, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 07, 2011 Sep 07, 2011
LATEST
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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources