Skip to main content
Gene_Godsey
Inspiring
February 18, 2011
Question

CFScript Math expression - Summing up looped over data

  • February 18, 2011
  • 2 replies
  • 1200 views

If I loop over numeric data and  I want to sum that data I am looking for the correct manner to do so. Of course I want to do this in cfscript.

I know that I could go the route of doing a one demensional array and using the array sum function and I could also use stauctures.

I am interested in different solutions to this problem.

My end state is that I need to sum and average looped dynamic data for reporting purposes.

For purposes of example static values will be helpful for better understanding.

Thanks in advance.

    This topic has been closed for replies.

    2 replies

    Inspiring
    February 18, 2011

    I have to say that Owain's reply is about as on the button as one can expect given the vagueness of your initial post.  And it's quite funny, in a patronising sort of way (I thought I was supposed to be the patronising one around here, according to you, Owain?)

    But is this data a *recordset*, you know, like form a CFQUERY (or similar) call?

    And is the data you want to sum a total of a COLUMN from said recordset?

    If so, one can just use arraySum(), eg:

    total = arraySum(queryname["columnName"]);

    However if there's any chance there are nulls in the column, you'll need to loop over the recordset and do the tally by hand, as arraySum() doesn't like null entries in its array.

    If it's something else, can you pls elaborate on what you're on about.

    --

    Adam

    Owainnorth
    Inspiring
    February 18, 2011
    Of course I want to do this in cfscript.

    Of course.

    For adding up numbers I would recommend using the + operator.

    For subtraction I would recommend the - operator.

    Really not sure what more you're after?

    Gene_Godsey
    Inspiring
    February 18, 2011

    LOL - Sorry for being Vague.

    I have a query that is looping out monthly number sets via a full year.

    Remember the key word here is looping. (Not static output.)

    I need to sum and average that information by it's complete output.

    Inspiring
    February 18, 2011

    I'm not sure how a query loops, but remember those array functions you mentioned in your first post?  They work on query objects.  The syntax is:

    YourAnswer = YourArrayFunction(YourQueryName["YourColumnName"]);