Best way to compute this average?

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Best way to compute this average?

Patrick Larkin-2
I have to compute an average based on 9 fields that either have a value from 0-3 or nothing.  If the field contains nothing (not zero), it needs to be removed from the average computation.  For instance:

value1 = 1
value2 =
value3 = 3
value4 = 0
value5 = 1
value6 = 2
value7 =
value8 = 0
value9 = 3

The correct computation would be `10/7 or 1.43

All of these values would be stored in a single record.  I'd need to test for emptiness, calculate a sum, and a divisor.  Is there a short way anyone can think of to do this?



Patrick Larkin
Information Systems
Bethlehem Area School District
https://www.beth.k12.pa.us





#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Best way to compute this average?

Jewett, Greg

Pseudo Code:

count = 0
sum = 0
iterate through loop
 if array (index) != (null)
count++
        sum = sum + array (index)
/if
average = sum / count;

You could create a quick function that accepts the array, then returns the average.  You can use that over and over.
No one-liners coming to mind.


________________________________
Greg Jewett
Senior Network and Domain Administrator
Signature Science, LLC
(512) 533-2027  [hidden email]<mailto:[hidden email]>
Save a tree!  Don't print this e-mail unless it's necessary.
Be the Solution!

On Nov 27, 2013, at 11:17 am, Patrick Larkin <[hidden email]<mailto:[hidden email]>> wrote:

I have to compute an average based on 9 fields that either have a value from 0-3 or nothing.  If the field contains nothing (not zero), it needs to be removed from the average computation.  For instance:

value1 = 1
value2 =
value3 = 3
value4 = 0
value5 = 1
value6 = 2
value7 =
value8 = 0
value9 = 3

The correct computation would be `10/7 or 1.43

All of these values would be stored in a single record.  I'd need to test for emptiness, calculate a sum, and a divisor.  Is there a short way anyone can think of to do this?



Patrick Larkin
Information Systems
Bethlehem Area School District
https://www.beth.k12.pa.us





#############################################################
This message is sent to you because you are subscribed to
 the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>


#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Best way to compute this average?

Brad Lindsay
In reply to this post by Patrick Larkin-2
If you're using Lasso 9, query expression:


local(myArray) = (:1,'',3,0,1,2,null,0,3)

with value in #myArray
where string(#value) != ''
average decimal(#value)


Adjust the where clause as necessary.

Brad


On 11/27/13, 12:17 PM, Patrick Larkin wrote:

> I have to compute an average based on 9 fields that either have a value from 0-3 or nothing.  If the field contains nothing (not zero), it needs to be removed from the average computation.  For instance:
>
> value1 = 1
> value2 =
> value3 = 3
> value4 = 0
> value5 = 1
> value6 = 2
> value7 =
> value8 = 0
> value9 = 3
>
> The correct computation would be `10/7 or 1.43
>
> All of these values would be stored in a single record.  I'd need to test for emptiness, calculate a sum, and a divisor.  Is there a short way anyone can think of to do this?
>
>
>
> Patrick Larkin
> Information Systems
> Bethlehem Area School District
> https://www.beth.k12.pa.us
>
>
>
>
>
> #############################################################
> This message is sent to you because you are subscribed to
>    the mailing list Lasso
> [hidden email]
> To unsubscribe, E-mail to:<[hidden email]>
> Send administrative queries to<[hidden email]>

#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Best way to compute this average?

Patrick Larkin-2
Sorry, Lasso 8.5.




On Nov 27, 2013, at 12:33 PM, Brad Lindsay <[hidden email]> wrote:

> If you're using Lasso 9, query expression:
>
>
> local(myArray) = (:1,'',3,0,1,2,null,0,3)
>
> with value in #myArray
> where string(#value) != ''
> average decimal(#value)
>
>
> Adjust the where clause as necessary.
>
> Brad
>
>
> On 11/27/13, 12:17 PM, Patrick Larkin wrote:
>> I have to compute an average based on 9 fields that either have a value from 0-3 or nothing.  If the field contains nothing (not zero), it needs to be removed from the average computation.  For instance:
>>
>> value1 = 1
>> value2 =
>> value3 = 3
>> value4 = 0
>> value5 = 1
>> value6 = 2
>> value7 =
>> value8 = 0
>> value9 = 3
>>
>> The correct computation would be `10/7 or 1.43
>>
>> All of these values would be stored in a single record.  I'd need to test for emptiness, calculate a sum, and a divisor.  Is there a short way anyone can think of to do this?
>>
>>
>>
>> Patrick Larkin
>> Information Systems
>> Bethlehem Area School District
>> https://www.beth.k12.pa.us
>>
>>
>>
>>
>>
>> #############################################################
>> This message is sent to you because you are subscribed to
>>   the mailing list Lasso
>> [hidden email]
>> To unsubscribe, E-mail to:<[hidden email]>
>> Send administrative queries to<[hidden email]>
>
> #############################################################
> This message is sent to you because you are subscribed to
> the mailing list Lasso
> [hidden email]
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>


#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Best way to compute this average?

Brad Lindsay
No need to apologize or feel sorry for me - you're the one on 8.5 :)

Brad


On 11/27/13, 1:01 PM, Patrick Larkin wrote:

> Sorry, Lasso 8.5.
>
>
>
>
> On Nov 27, 2013, at 12:33 PM, Brad Lindsay<[hidden email]>  wrote:
>
>> If you're using Lasso 9, query expression:
>>
>>
>> local(myArray) = (:1,'',3,0,1,2,null,0,3)
>>
>> with value in #myArray
>> where string(#value) != ''
>> average decimal(#value)
>>
>>
>> Adjust the where clause as necessary.
>>
>> Brad
>>
>>
>> On 11/27/13, 12:17 PM, Patrick Larkin wrote:
>>> I have to compute an average based on 9 fields that either have a value from 0-3 or nothing.  If the field contains nothing (not zero), it needs to be removed from the average computation.  For instance:
>>>
>>> value1 = 1
>>> value2 =
>>> value3 = 3
>>> value4 = 0
>>> value5 = 1
>>> value6 = 2
>>> value7 =
>>> value8 = 0
>>> value9 = 3
>>>
>>> The correct computation would be `10/7 or 1.43
>>>
>>> All of these values would be stored in a single record.  I'd need to test for emptiness, calculate a sum, and a divisor.  Is there a short way anyone can think of to do this?
>>>
>>>
>>>
>>> Patrick Larkin
>>> Information Systems
>>> Bethlehem Area School District
>>> https://www.beth.k12.pa.us
>>>
>>>
>>>
>>>
>>>
>>> #############################################################
>>> This message is sent to you because you are subscribed to
>>>    the mailing list Lasso
>>> [hidden email]
>>> To unsubscribe, E-mail to:<[hidden email]>
>>> Send administrative queries to<[hidden email]>
>> #############################################################
>> This message is sent to you because you are subscribed to
>> the mailing list Lasso
>> [hidden email]
>> To unsubscribe, E-mail to:<[hidden email]>
>> Send administrative queries to<[hidden email]>
>
>
> #############################################################
> This message is sent to you because you are subscribed to
>    the mailing list Lasso
> [hidden email]
> To unsubscribe, E-mail to:<[hidden email]>
> Send administrative queries to<[hidden email]>

#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Best way to compute this average?

Tim Taplin
In reply to this post by Patrick Larkin-2
Not really short, but assuming that your values stored in one record, as you mentioned, are available as fields, this should work. Likely you were doing something like this and wished for a shorter method. I was trying to think of a query or sql method to shorten this but not having any luck at the moment. For example, it likely would be possible to use an sql statement that could inspect the listed fields and return a custom field with the count of nonempty columns as well as a field with the sum of those columns. This might wind up being more tedious than the below code.


local('value_array' = array());
field('value1') != '' ? #value_array->insert(field('value1'));
field('value2') != '' ? #value_array->insert(field('value2'));
field('value3') != '' ? #value_array->insert(field('value3'));
field('value4') != '' ? #value_array->insert(field('value4'));
field('value5') != '' ? #value_array->insert(field('value5'));
field('value6') != '' ? #value_array->insert(field('value6'));
field('value7') != '' ? #value_array->insert(field('value7'));
field('value8') != '' ? #value_array->insert(field('value8'));
field('value9') != '' ? #value_array->insert(field('value9'));

local('sum_values' = 0);
loop(#value_array->size);
        #sum_values+=#value_array->get(loop_count);
/loop;
local('value_average' = #sum_values/#value_array->size);

On Nov 27, 2013, at 11:01 AM, Patrick Larkin <[hidden email]> wrote:

> Sorry, Lasso 8.5.
>
>
>
>
> On Nov 27, 2013, at 12:33 PM, Brad Lindsay <[hidden email]> wrote:
>
>> If you're using Lasso 9, query expression:
>>
>>
>> local(myArray) = (:1,'',3,0,1,2,null,0,3)
>>
>> with value in #myArray
>> where string(#value) != ''
>> average decimal(#value)
>>
>>
>> Adjust the where clause as necessary.
>>
>> Brad
>>
>>
>> On 11/27/13, 12:17 PM, Patrick Larkin wrote:
>>> I have to compute an average based on 9 fields that either have a value from 0-3 or nothing.  If the field contains nothing (not zero), it needs to be removed from the average computation.  For instance:
>>>
>>> value1 = 1
>>> value2 =
>>> value3 = 3
>>> value4 = 0
>>> value5 = 1
>>> value6 = 2
>>> value7 =
>>> value8 = 0
>>> value9 = 3
>>>
>>> The correct computation would be `10/7 or 1.43
>>>
>>> All of these values would be stored in a single record.  I'd need to test for emptiness, calculate a sum, and a divisor.  Is there a short way anyone can think of to do this?
>>>
>>>
>>>
>>> Patrick Larkin
>>> Information Systems
>>> Bethlehem Area School District
>>> https://www.beth.k12.pa.us
>>>
>>>
>>>
>>>
>>>
>>> #############################################################
>>> This message is sent to you because you are subscribed to
>>>  the mailing list Lasso
>>> [hidden email]
>>> To unsubscribe, E-mail to:<[hidden email]>
>>> Send administrative queries to<[hidden email]>
>>
>> #############################################################
>> This message is sent to you because you are subscribed to
>> the mailing list Lasso
>> [hidden email]
>> To unsubscribe, E-mail to: <[hidden email]>
>> Send administrative queries to  <[hidden email]>
>
>
> #############################################################
> This message is sent to you because you are subscribed to
>  the mailing list Lasso
> [hidden email]
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>


#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Best way to compute this average?

stevepiercy
This version iterates over an array of field names, casts values
to integers (in 8, all fields are returned as type string and
must be cast to their appropriate type, whereas in 9 fields are
returned as their type in the database schema most of the time),
keeps a sum and a count of integer values, and avoids a divide
by zero error.

[
local('f' = array('value1','value2','value3','value4','value5','value6','value7','value8','value9'));
//local('f' = array('1','','3','0','1','2','','0','3')); //
toggle comment for testing

local('sum' = 0,
     'count' = 0);

iterate(#f, local('i'));
     if(field(#i) != '');
         #sum+=integer(field(#i));

/*
// toggle comment for testing
     if(#i != '');
         #sum+=integer(#i);
  */
         #count+=1;
     /if;
/iterate;

if(#count > 0);
     // prevent divide by zero error
     'The correct computation would be ' +#sum + '/' + #count +
' or ';
     (decimal(#sum)/decimal(#count))->setformat(-precision=2)&;
else;
     'cannot average zero elements';
/if;
]

--steve


On 11/27/13 at 11:58 AM, [hidden email] (Tim Taplin) pronounced:

>Not really short, but assuming that your values stored in one
>record, as you mentioned, are available as fields, this should
>work. Likely you were doing something like this and wished for
>a shorter method. I was trying to think of a query or sql
>method to shorten this but not having any luck at the moment.
>For example, it likely would be possible to use an sql
>statement that could inspect the listed fields and return a
>custom field with the count of nonempty columns as well as a
>field with the sum of those columns. This might wind up being
>more tedious than the below code.
>
>
>local('value_array' = array());
>field('value1') != '' ? #value_array->insert(field('value1'));
>field('value2') != '' ? #value_array->insert(field('value2'));
>field('value3') != '' ? #value_array->insert(field('value3'));
>field('value4') != '' ? #value_array->insert(field('value4'));
>field('value5') != '' ? #value_array->insert(field('value5'));
>field('value6') != '' ? #value_array->insert(field('value6'));
>field('value7') != '' ? #value_array->insert(field('value7'));
>field('value8') != '' ? #value_array->insert(field('value8'));
>field('value9') != '' ? #value_array->insert(field('value9'));
>
>local('sum_values' = 0);
>loop(#value_array->size);
>#sum_values+=#value_array->get(loop_count);
>/loop;
>local('value_average' = #sum_values/#value_array->size);
>
>On Nov 27, 2013, at 11:01 AM, Patrick Larkin <[hidden email]> wrote:
>
>>Sorry, Lasso 8.5.
>>
>>
>>
>>
>>On Nov 27, 2013, at 12:33 PM, Brad Lindsay <[hidden email]> wrote:
>>
>>> If you're using Lasso 9, query expression:
>>>    local(myArray) = (:1,'',3,0,1,2,null,0,3)
>>>   with value in #myArray
>>> where string(#value) != ''
>>> average decimal(#value)
>>>    Adjust the where clause as necessary.
>>>   Brad
>>>    On 11/27/13, 12:17 PM, Patrick Larkin wrote:
>>>> I have to compute an average based on 9 fields that either have a value from 0-3 or nothing.  If
>the field contains nothing (not zero), it needs to be removed
>from the average computation.  For instance:
>>>>   value1 = 1
>>>> value2 =
>>>> value3 = 3
>>>> value4 = 0
>>>> value5 = 1
>>>> value6 = 2
>>>> value7 =
>>>> value8 = 0
>>>> value9 = 3
>>>>   The correct computation would be `10/7 or 1.43
>>>>   All of these values would be stored in a single record.  
>>>>I'd need to test for emptiness, calculate
>a sum, and a divisor.  Is there a short way anyone can think of to do this?
>>>>     Patrick Larkin
>>>> Information Systems
>>>> Bethlehem Area School District
>>>> https://www.beth.k12.pa.us
>>>>       #############################################################
>>>> This message is sent to you because you are subscribed to
>>>>  the mailing list Lasso
>>>> [hidden email]
>>>> To unsubscribe, E-mail to:<[hidden email]>
>>>> Send administrative queries to<[hidden email]>
>>>   #############################################################
>>> This message is sent to you because you are subscribed to
>>> the mailing list Lasso
>>> [hidden email]
>>> To unsubscribe, E-mail to: <[hidden email]>
>>> Send administrative queries to  <[hidden email]>
>>
>>
>>#############################################################
>>This message is sent to you because you are subscribed to
>>the mailing list Lasso
>>[hidden email]
>>To unsubscribe, E-mail to: <[hidden email]>
>>Send administrative queries to  <[hidden email]>
>
>
>#############################################################
>This message is sent to you because you are subscribed to
>the mailing list Lasso
>[hidden email]
>To unsubscribe, E-mail to: <[hidden email]>
>Send administrative queries to  <[hidden email]>

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- --
Steve Piercy               Web Site Builder              
Soquel, CA
<[hidden email]>                  <http://www.StevePiercy.com/>


#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Best way to compute this average?

Patrick Larkin-2
Hello -

Thanks for this response.  I appreciate it.  I'm having a little trouble understanding this part:

> iterate(#f, local('i'));
>    if(field(#i) != '');
>        #sum+=integer(field(#i));
>        #count+=1;
>    /if;
> /iterate;


I retrieved a record, placed it in an array (f) and am able to echo the array so I know it's contents are correct.  The above code references "field" inside the iterate.  When I use this, both the sum and count remain 0.   I've never referenced an array with "field" before.  Am I doing something wrong or is there something additional required?  

Patrick Larkin
Information Systems
Bethlehem Area School District
https://www.beth.k12.pa.us




On Nov 27, 2013, at 4:32 PM, Steve Piercy - Web Site Builder <[hidden email]> wrote:

> This version iterates over an array of field names, casts values to integers (in 8, all fields are returned as type string and must be cast to their appropriate type, whereas in 9 fields are returned as their type in the database schema most of the time), keeps a sum and a count of integer values, and avoids a divide by zero error.
>
> [
> local('f' = array('value1','value2','value3','value4','value5','value6','value7','value8','value9'));
> //local('f' = array('1','','3','0','1','2','','0','3')); // toggle comment for testing
>
> local('sum' = 0,
>    'count' = 0);
>
> iterate(#f, local('i'));
>    if(field(#i) != '');
>        #sum+=integer(field(#i));
>
> /*
> // toggle comment for testing
>    if(#i != '');
>        #sum+=integer(#i);
> */
>        #count+=1;
>    /if;
> /iterate;
>
> if(#count > 0);
>    // prevent divide by zero error
>    'The correct computation would be ' +#sum + '/' + #count + ' or ';
>    (decimal(#sum)/decimal(#count))->setformat(-precision=2)&;
> else;
>    'cannot average zero elements';
> /if;
> ]
>
> --steve
>
>
> On 11/27/13 at 11:58 AM, [hidden email] (Tim Taplin) pronounced:
>
>> Not really short, but assuming that your values stored in one record, as you mentioned, are available as fields, this should work. Likely you were doing something like this and wished for a shorter method. I was trying to think of a query or sql method to shorten this but not having any luck at the moment. For example, it likely would be possible to use an sql statement that could inspect the listed fields and return a custom field with the count of nonempty columns as well as a field with the sum of those columns. This might wind up being more tedious than the below code.
>>
>>
>> local('value_array' = array());
>> field('value1') != '' ? #value_array->insert(field('value1'));
>> field('value2') != '' ? #value_array->insert(field('value2'));
>> field('value3') != '' ? #value_array->insert(field('value3'));
>> field('value4') != '' ? #value_array->insert(field('value4'));
>> field('value5') != '' ? #value_array->insert(field('value5'));
>> field('value6') != '' ? #value_array->insert(field('value6'));
>> field('value7') != '' ? #value_array->insert(field('value7'));
>> field('value8') != '' ? #value_array->insert(field('value8'));
>> field('value9') != '' ? #value_array->insert(field('value9'));
>>
>> local('sum_values' = 0);
>> loop(#value_array->size);
>> #sum_values+=#value_array->get(loop_count);
>> /loop;
>> local('value_average' = #sum_values/#value_array->size);
>>
>> On Nov 27, 2013, at 11:01 AM, Patrick Larkin <[hidden email]> wrote:
>>
>>> Sorry, Lasso 8.5.
>>>
>>>
>>>
>>>
>>> On Nov 27, 2013, at 12:33 PM, Brad Lindsay <[hidden email]> wrote:
>>>
>>>> If you're using Lasso 9, query expression:
>>>>   local(myArray) = (:1,'',3,0,1,2,null,0,3)
>>>>  with value in #myArray
>>>> where string(#value) != ''
>>>> average decimal(#value)
>>>>   Adjust the where clause as necessary.
>>>>  Brad
>>>>   On 11/27/13, 12:17 PM, Patrick Larkin wrote:
>>>>> I have to compute an average based on 9 fields that either have a value from 0-3 or nothing.  If
>> the field contains nothing (not zero), it needs to be removed from the average computation.  For instance:
>>>>>  value1 = 1
>>>>> value2 =
>>>>> value3 = 3
>>>>> value4 = 0
>>>>> value5 = 1
>>>>> value6 = 2
>>>>> value7 =
>>>>> value8 = 0
>>>>> value9 = 3
>>>>>  The correct computation would be `10/7 or 1.43
>>>>>  All of these values would be stored in a single record.  I'd need to test for emptiness, calculate
>> a sum, and a divisor.  Is there a short way anyone can think of to do this?
>>>>>    Patrick Larkin
>>>>> Information Systems
>>>>> Bethlehem Area School District
>>>>> https://www.beth.k12.pa.us
>>>>>      #############################################################
>>>>> This message is sent to you because you are subscribed to
>>>>> the mailing list Lasso
>>>>> [hidden email]
>>>>> To unsubscribe, E-mail to:<[hidden email]>
>>>>> Send administrative queries to<[hidden email]>
>>>>  #############################################################
>>>> This message is sent to you because you are subscribed to
>>>> the mailing list Lasso
>>>> [hidden email]
>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>> Send administrative queries to  <[hidden email]>
>>>
>>>
>>> #############################################################
>>> This message is sent to you because you are subscribed to
>>> the mailing list Lasso
>>> [hidden email]
>>> To unsubscribe, E-mail to: <[hidden email]>
>>> Send administrative queries to  <[hidden email]>
>>
>>
>> #############################################################
>> This message is sent to you because you are subscribed to
>> the mailing list Lasso
>> [hidden email]
>> To unsubscribe, E-mail to: <[hidden email]>
>> Send administrative queries to  <[hidden email]>
>
> -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
> Steve Piercy               Web Site Builder               Soquel, CA
> <[hidden email]>                  <http://www.StevePiercy.com/>
>
>
> #############################################################
> This message is sent to you because you are subscribed to
> the mailing list Lasso
> [hidden email]
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>


#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Best way to compute this average?

stevepiercy
Don't put the record in an array.  Do this:

    records;
        iterate(#f, local('i'));
           if(field(#i) != '');
               #sum+=integer(field(#i));
               #count+=1;
           /if;
        /iterate;
    /records;

--steve


On 12/2/13 at 9:38 AM, [hidden email] (Patrick Larkin) pronounced:

> Hello -
>
> Thanks for this response.  I appreciate it.  I'm having a little trouble
> understanding this part:
>
> > iterate(#f, local('i'));
> >    if(field(#i) != '');
> >        #sum+=integer(field(#i));
> >        #count+=1;
> >    /if;
> > /iterate;
>
>
> I retrieved a record, placed it in an array (f) and am able to echo the array so I
> know it's contents are correct.  The above code references "field" inside the
> iterate.  When I use this, both the sum and count remain 0.   I've never referenced
> an array with "field" before.  Am I doing something wrong or is there something
> additional required?  
>
> Patrick Larkin
> Information Systems
> Bethlehem Area School District
> https://www.beth.k12.pa.us
>
>
>
>
> On Nov 27, 2013, at 4:32 PM, Steve Piercy - Web Site Builder <[hidden email]>
> wrote:
>
> > This version iterates over an array of field names, casts values to integers (in
> 8, all fields are returned as type string and must be cast to their appropriate
> type, whereas in 9 fields are returned as their type in the database schema most of
> the time), keeps a sum and a count of integer values, and avoids a divide by zero
> error.
> >
> > [
> > local('f' =
> array('value1','value2','value3','value4','value5','value6','value7','value8','
> value9'));
> > //local('f' = array('1','','3','0','1','2','','0','3')); // toggle comment for
> testing
> >
> > local('sum' = 0,
> >    'count' = 0);
> >
> > iterate(#f, local('i'));
> >    if(field(#i) != '');
> >        #sum+=integer(field(#i));
> >
> > /*
> > // toggle comment for testing
> >    if(#i != '');
> >        #sum+=integer(#i);
> > */
> >        #count+=1;
> >    /if;
> > /iterate;
> >
> > if(#count > 0);
> >    // prevent divide by zero error
> >    'The correct computation would be ' +#sum + '/' + #count + ' or ';
> >    (decimal(#sum)/decimal(#count))->setformat(-precision=2)&;
> > else;
> >    'cannot average zero elements';
> > /if;
> > ]
> >
> > --steve
> >
> >
> > On 11/27/13 at 11:58 AM, [hidden email] (Tim Taplin) pronounced:
> >
> >> Not really short, but assuming that your values stored in one record, as you
> mentioned, are available as fields, this should work. Likely you were doing
> something like this and wished for a shorter method. I was trying to think of a
> query or sql method to shorten this but not having any luck at the moment. For
> example, it likely would be possible to use an sql statement that could inspect the
> listed fields and return a custom field with the count of nonempty columns as well
> as a field with the sum of those columns. This might wind up being more tedious
> than the below code.
> >>
> >>
> >> local('value_array' = array());
> >> field('value1') != '' ? #value_array->insert(field('value1'));
> >> field('value2') != '' ? #value_array->insert(field('value2'));
> >> field('value3') != '' ? #value_array->insert(field('value3'));
> >> field('value4') != '' ? #value_array->insert(field('value4'));
> >> field('value5') != '' ? #value_array->insert(field('value5'));
> >> field('value6') != '' ? #value_array->insert(field('value6'));
> >> field('value7') != '' ? #value_array->insert(field('value7'));
> >> field('value8') != '' ? #value_array->insert(field('value8'));
> >> field('value9') != '' ? #value_array->insert(field('value9'));
> >>
> >> local('sum_values' = 0);
> >> loop(#value_array->size);
> >> #sum_values+=#value_array->get(loop_count);
> >> /loop;
> >> local('value_average' = #sum_values/#value_array->size);
> >>
> >> On Nov 27, 2013, at 11:01 AM, Patrick Larkin <[hidden email]> wrote:
> >>
> >>> Sorry, Lasso 8.5.
> >>>
> >>>
> >>>
> >>>
> >>> On Nov 27, 2013, at 12:33 PM, Brad Lindsay <[hidden email]> wrote:
> >>>
> >>>> If you're using Lasso 9, query expression:
> >>>>   local(myArray) = (:1,'',3,0,1,2,null,0,3)
> >>>>  with value in #myArray
> >>>> where string(#value) != ''
> >>>> average decimal(#value)
> >>>>   Adjust the where clause as necessary.
> >>>>  Brad
> >>>>   On 11/27/13, 12:17 PM, Patrick Larkin wrote:
> >>>>> I have to compute an average based on 9 fields that either have a value from
> 0-3 or nothing.  If
> >> the field contains nothing (not zero), it needs to be removed from the average
> computation.  For instance:
> >>>>>  value1 = 1
> >>>>> value2 =
> >>>>> value3 = 3
> >>>>> value4 = 0
> >>>>> value5 = 1
> >>>>> value6 = 2
> >>>>> value7 =
> >>>>> value8 = 0
> >>>>> value9 = 3
> >>>>>  The correct computation would be `10/7 or 1.43
> >>>>>  All of these values would be stored in a single record.  I'd need to test
> for emptiness, calculate
> >> a sum, and a divisor.  Is there a short way anyone can think of to do this?
> >>>>>    Patrick Larkin
> >>>>> Information Systems
> >>>>> Bethlehem Area School District
> >>>>> https://www.beth.k12.pa.us
> >>>>>      #############################################################
> >>>>> This message is sent to you because you are subscribed to
> >>>>> the mailing list Lasso
> >>>>> [hidden email]
> >>>>> To unsubscribe, E-mail to:<[hidden email]>
> >>>>> Send administrative queries to<[hidden email]>
> >>>>  #############################################################
> >>>> This message is sent to you because you are subscribed to
> >>>> the mailing list Lasso
> >>>> [hidden email]
> >>>> To unsubscribe, E-mail to: <[hidden email]>
> >>>> Send administrative queries to  <[hidden email]>
> >>>
> >>>
> >>> #############################################################
> >>> This message is sent to you because you are subscribed to
> >>> the mailing list Lasso
> >>> [hidden email]
> >>> To unsubscribe, E-mail to: <[hidden email]>
> >>> Send administrative queries to  <[hidden email]>
> >>
> >>
> >> #############################################################
> >> This message is sent to you because you are subscribed to
> >> the mailing list Lasso
> >> [hidden email]
> >> To unsubscribe, E-mail to: <[hidden email]>
> >> Send administrative queries to  <[hidden email]>
> >
> > -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
> > Steve Piercy               Web Site Builder               Soquel, CA
> > <[hidden email]>                  <http://www.StevePiercy.com/>
> >
> >
> > #############################################################
> > This message is sent to you because you are subscribed to
> > the mailing list Lasso
> > [hidden email]
> > To unsubscribe, E-mail to: <[hidden email]>
> > Send administrative queries to  <[hidden email]>
>
>
> #############################################################
> This message is sent to you because you are subscribed to
>   the mailing list Lasso
> [hidden email]
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Steve Piercy               Web Site Builder               Soquel, CA
<[hidden email]>                  <http://www.StevePiercy.com/>


#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Best way to compute this average?

Patrick Larkin-2
Still same outcome.  Sum and count are 0.  

I'll try and figure something out.  Thanks.


On Dec 2, 2013, at 9:45 AM, Steve Piercy - Web Site Builder <[hidden email]> wrote:

> Don't put the record in an array.  Do this:
>
>    records;
>        iterate(#f, local('i'));
>           if(field(#i) != '');
>               #sum+=integer(field(#i));
>               #count+=1;
>           /if;
>        /iterate;
>    /records;
>
> --steve
>
>
> On 12/2/13 at 9:38 AM, [hidden email] (Patrick Larkin) pronounced:
>
>> Hello -
>>
>> Thanks for this response.  I appreciate it.  I'm having a little trouble
>> understanding this part:
>>
>>> iterate(#f, local('i'));
>>>   if(field(#i) != '');
>>>       #sum+=integer(field(#i));
>>>       #count+=1;
>>>   /if;
>>> /iterate;
>>
>>
>> I retrieved a record, placed it in an array (f) and am able to echo the array so I
>> know it's contents are correct.  The above code references "field" inside the
>> iterate.  When I use this, both the sum and count remain 0.   I've never referenced
>> an array with "field" before.  Am I doing something wrong or is there something
>> additional required?  
>>
>> Patrick Larkin
>> Information Systems
>> Bethlehem Area School District
>> https://www.beth.k12.pa.us
>>
>>
>>
>>
>> On Nov 27, 2013, at 4:32 PM, Steve Piercy - Web Site Builder <[hidden email]>
>> wrote:
>>
>>> This version iterates over an array of field names, casts values to integers (in
>> 8, all fields are returned as type string and must be cast to their appropriate
>> type, whereas in 9 fields are returned as their type in the database schema most of
>> the time), keeps a sum and a count of integer values, and avoids a divide by zero
>> error.
>>>
>>> [
>>> local('f' =
>> array('value1','value2','value3','value4','value5','value6','value7','value8','
>> value9'));
>>> //local('f' = array('1','','3','0','1','2','','0','3')); // toggle comment for
>> testing
>>>
>>> local('sum' = 0,
>>>   'count' = 0);
>>>
>>> iterate(#f, local('i'));
>>>   if(field(#i) != '');
>>>       #sum+=integer(field(#i));
>>>
>>> /*
>>> // toggle comment for testing
>>>   if(#i != '');
>>>       #sum+=integer(#i);
>>> */
>>>       #count+=1;
>>>   /if;
>>> /iterate;
>>>
>>> if(#count > 0);
>>>   // prevent divide by zero error
>>>   'The correct computation would be ' +#sum + '/' + #count + ' or ';
>>>   (decimal(#sum)/decimal(#count))->setformat(-precision=2)&;
>>> else;
>>>   'cannot average zero elements';
>>> /if;
>>> ]
>>>
>>> --steve
>>>
>>>
>>> On 11/27/13 at 11:58 AM, [hidden email] (Tim Taplin) pronounced:
>>>
>>>> Not really short, but assuming that your values stored in one record, as you
>> mentioned, are available as fields, this should work. Likely you were doing
>> something like this and wished for a shorter method. I was trying to think of a
>> query or sql method to shorten this but not having any luck at the moment. For
>> example, it likely would be possible to use an sql statement that could inspect the
>> listed fields and return a custom field with the count of nonempty columns as well
>> as a field with the sum of those columns. This might wind up being more tedious
>> than the below code.
>>>>
>>>>
>>>> local('value_array' = array());
>>>> field('value1') != '' ? #value_array->insert(field('value1'));
>>>> field('value2') != '' ? #value_array->insert(field('value2'));
>>>> field('value3') != '' ? #value_array->insert(field('value3'));
>>>> field('value4') != '' ? #value_array->insert(field('value4'));
>>>> field('value5') != '' ? #value_array->insert(field('value5'));
>>>> field('value6') != '' ? #value_array->insert(field('value6'));
>>>> field('value7') != '' ? #value_array->insert(field('value7'));
>>>> field('value8') != '' ? #value_array->insert(field('value8'));
>>>> field('value9') != '' ? #value_array->insert(field('value9'));
>>>>
>>>> local('sum_values' = 0);
>>>> loop(#value_array->size);
>>>> #sum_values+=#value_array->get(loop_count);
>>>> /loop;
>>>> local('value_average' = #sum_values/#value_array->size);
>>>>
>>>> On Nov 27, 2013, at 11:01 AM, Patrick Larkin <[hidden email]> wrote:
>>>>
>>>>> Sorry, Lasso 8.5.
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> On Nov 27, 2013, at 12:33 PM, Brad Lindsay <[hidden email]> wrote:
>>>>>
>>>>>> If you're using Lasso 9, query expression:
>>>>>>  local(myArray) = (:1,'',3,0,1,2,null,0,3)
>>>>>> with value in #myArray
>>>>>> where string(#value) != ''
>>>>>> average decimal(#value)
>>>>>>  Adjust the where clause as necessary.
>>>>>> Brad
>>>>>>  On 11/27/13, 12:17 PM, Patrick Larkin wrote:
>>>>>>> I have to compute an average based on 9 fields that either have a value from
>> 0-3 or nothing.  If
>>>> the field contains nothing (not zero), it needs to be removed from the average
>> computation.  For instance:
>>>>>>> value1 = 1
>>>>>>> value2 =
>>>>>>> value3 = 3
>>>>>>> value4 = 0
>>>>>>> value5 = 1
>>>>>>> value6 = 2
>>>>>>> value7 =
>>>>>>> value8 = 0
>>>>>>> value9 = 3
>>>>>>> The correct computation would be `10/7 or 1.43
>>>>>>> All of these values would be stored in a single record.  I'd need to test
>> for emptiness, calculate
>>>> a sum, and a divisor.  Is there a short way anyone can think of to do this?
>>>>>>>   Patrick Larkin
>>>>>>> Information Systems
>>>>>>> Bethlehem Area School District
>>>>>>> https://www.beth.k12.pa.us
>>>>>>>     #############################################################
>>>>>>> This message is sent to you because you are subscribed to
>>>>>>> the mailing list Lasso
>>>>>>> [hidden email]
>>>>>>> To unsubscribe, E-mail to:<[hidden email]>
>>>>>>> Send administrative queries to<[hidden email]>
>>>>>> #############################################################
>>>>>> This message is sent to you because you are subscribed to
>>>>>> the mailing list Lasso
>>>>>> [hidden email]
>>>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>>>> Send administrative queries to  <[hidden email]>
>>>>>
>>>>>
>>>>> #############################################################
>>>>> This message is sent to you because you are subscribed to
>>>>> the mailing list Lasso
>>>>> [hidden email]
>>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>>> Send administrative queries to  <[hidden email]>
>>>>
>>>>
>>>> #############################################################
>>>> This message is sent to you because you are subscribed to
>>>> the mailing list Lasso
>>>> [hidden email]
>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>> Send administrative queries to  <[hidden email]>
>>>
>>> -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
>>> Steve Piercy               Web Site Builder               Soquel, CA
>>> <[hidden email]>                  <http://www.StevePiercy.com/>
>>>
>>>
>>> #############################################################
>>> This message is sent to you because you are subscribed to
>>> the mailing list Lasso
>>> [hidden email]
>>> To unsubscribe, E-mail to: <[hidden email]>
>>> Send administrative queries to  <[hidden email]>
>>
>>
>> #############################################################
>> This message is sent to you because you are subscribed to
>>  the mailing list Lasso
>> [hidden email]
>> To unsubscribe, E-mail to: <[hidden email]>
>> Send administrative queries to  <[hidden email]>
>
> -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
> Steve Piercy               Web Site Builder               Soquel, CA
> <[hidden email]>                  <http://www.StevePiercy.com/>
>
>
> #############################################################
> This message is sent to you because you are subscribed to
>  the mailing list Lasso
> [hidden email]
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>


#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Best way to compute this average?

stevepiercy
Let's see the code.  It should look like this:

[
inline(...moose...);
     local('f' = array('value1','value2','value3','value4','value5','value6','value7','value8','value9'));

     local('sum' = 0,
         'count' = 0);
     records;
         iterate(#f, local('i'));
             if(field(#i) != '');
                 #sum+=integer(field(#i));
                 #count+=1;
             /if;
         /iterate;
     /records;
/inline;
if(#count > 0);
     // prevent divide by zero error
     'The correct computation would be ' +#sum + '/' + #count +
' or ';
     (decimal(#sum)/decimal(#count))->setformat(-precision=2)&;
else;
     'cannot average zero elements';
/if;
]

--steve

On 12/2/13 at 10:06 AM, [hidden email] (Patrick Larkin) pronounced:

>Still same outcome.  Sum and count are 0.
>I'll try and figure something out.  Thanks.
>
>
>On Dec 2, 2013, at 9:45 AM, Steve Piercy - Web Site Builder
><[hidden email]> wrote:
>
>>Don't put the record in an array.  Do this:
>>
>>records;
>>iterate(#f, local('i'));
>>if(field(#i) != '');
>>#sum+=integer(field(#i));
>>#count+=1;
>>/if;
>>/iterate;
>>/records;
>>
>>--steve
>>
>>
>>On 12/2/13 at 9:38 AM, [hidden email] (Patrick Larkin) pronounced:
>>
>>> Hello -
>>>   Thanks for this response.  I appreciate it.  I'm having a
>>>little trouble  understanding this part:
>>>
>>>> iterate(#f, local('i'));
>>>>   if(field(#i) != '');
>>>>       #sum+=integer(field(#i));
>>>>       #count+=1;
>>>>   /if;
>>>> /iterate;
>>>    I retrieved a record, placed it in an array (f) and am
>>>able to echo the array so
>I
>>>  know it's contents are correct.  The above code references
>>>"field" inside the  iterate.  When I use this, both the sum
>>>and count remain 0.   I've never
>referenced
>>>  an array with "field" before.  Am I doing something wrong
>>>or is there something  additional required?    Patrick Larkin
>>> Information Systems
>>> Bethlehem Area School District
>>> https://www.beth.k12.pa.us
>>>      On Nov 27, 2013, at 4:32 PM, Steve Piercy - Web Site Builder
><[hidden email]>
>>> wrote:
>>>
>>>> This version iterates over an array of field names, casts values to integers
>(in
>>>  8, all fields are returned as type string and must be cast
>>>to their appropriate  type, whereas in 9 fields are returned
>>>as their type in the database schema most
>of
>>> the time), keeps a sum and a count of integer values, and avoids a divide by
>zero
>>> error.
>>>>   [
>>>> local('f' =
>>> array('value1','value2','value3','value4','value5','value6','value7','value8','
>>> value9'));
>>>> //local('f' = array('1','','3','0','1','2','','0','3')); // toggle comment for
>>> testing
>>>>   local('sum' = 0,
>>>>   'count' = 0);
>>>>   iterate(#f, local('i'));
>>>>   if(field(#i) != '');
>>>>       #sum+=integer(field(#i));
>>>>   /*
>>>> // toggle comment for testing
>>>>   if(#i != '');
>>>>       #sum+=integer(#i);
>>>> */
>>>>       #count+=1;
>>>>   /if;
>>>> /iterate;
>>>>   if(#count > 0);
>>>>   // prevent divide by zero error
>>>>   'The correct computation would be ' +#sum + '/' + #count + ' or ';
>>>>   (decimal(#sum)/decimal(#count))->setformat(-precision=2)&;
>>>> else;
>>>>   'cannot average zero elements';
>>>> /if;
>>>> ]
>>>>   --steve
>>>>    On 11/27/13 at 11:58 AM, [hidden email] (Tim
>>>>Taplin) pronounced:
>>>>
>>>>> Not really short, but assuming that your values stored in one record, as you
>>>  mentioned, are available as fields, this should work.
>>>Likely you were doing  something like this and wished for a
>>>shorter method. I was trying to think of a  query or sql
>>>method to shorten this but not having any luck at the moment.
>>>For  example, it likely would be possible to use an sql
>>>statement that could inspect
>the
>>> listed fields and return a custom field with the count of nonempty columns as
>well
>>>  as a field with the sum of those columns. This might wind
>>>up being more tedious  than the below code.
>>>>>    local('value_array' = array());
>>>>> field('value1') != '' ? #value_array->insert(field('value1'));
>>>>> field('value2') != '' ? #value_array->insert(field('value2'));
>>>>> field('value3') != '' ? #value_array->insert(field('value3'));
>>>>> field('value4') != '' ? #value_array->insert(field('value4'));
>>>>> field('value5') != '' ? #value_array->insert(field('value5'));
>>>>> field('value6') != '' ? #value_array->insert(field('value6'));
>>>>> field('value7') != '' ? #value_array->insert(field('value7'));
>>>>> field('value8') != '' ? #value_array->insert(field('value8'));
>>>>> field('value9') != '' ? #value_array->insert(field('value9'));
>>>>>   local('sum_values' = 0);
>>>>> loop(#value_array->size);
>>>>> #sum_values+=#value_array->get(loop_count);
>>>>> /loop;
>>>>> local('value_average' = #sum_values/#value_array->size);
>>>>>   On Nov 27, 2013, at 11:01 AM, Patrick Larkin
>>>>><[hidden email]> wrote:
>>>>>
>>>>>> Sorry, Lasso 8.5.
>>>>>>      On Nov 27, 2013, at 12:33 PM, Brad Lindsay
>>>>>><[hidden email]> wrote:
>>>>>>
>>>>>>> If you're using Lasso 9, query expression:
>>>>>>>  local(myArray) = (:1,'',3,0,1,2,null,0,3)
>>>>>>> with value in #myArray
>>>>>>> where string(#value) != ''
>>>>>>> average decimal(#value)
>>>>>>>  Adjust the where clause as necessary.
>>>>>>> Brad
>>>>>>>  On 11/27/13, 12:17 PM, Patrick Larkin wrote:
>>>>>>>> I have to compute an average based on 9 fields that either have a value
>from
>>> 0-3 or nothing.  If
>>>>> the field contains nothing (not zero), it needs to be removed from the average
>>> computation.  For instance:
>>>>>>>> value1 = 1
>>>>>>>> value2 =
>>>>>>>> value3 = 3
>>>>>>>> value4 = 0
>>>>>>>> value5 = 1
>>>>>>>> value6 = 2
>>>>>>>> value7 =
>>>>>>>> value8 = 0
>>>>>>>> value9 = 3
>>>>>>>> The correct computation would be `10/7 or 1.43
>>>>>>>> All of these values would be stored in a single record.  I'd need to test
>>> for emptiness, calculate
>>>>> a sum, and a divisor.  Is there a short way anyone can think of to do this?
>>>>>>>>   Patrick Larkin
>>>>>>>> Information Systems
>>>>>>>> Bethlehem Area School District
>>>>>>>> https://www.beth.k12.pa.us
>>>>>>>>     #############################################################
>>>>>>>> This message is sent to you because you are subscribed to
>>>>>>>> the mailing list Lasso
>>>>>>>> [hidden email]
>>>>>>>> To unsubscribe, E-mail to:<[hidden email]>
>>>>>>>> Send administrative queries to<[hidden email]>
>>>>>>> #############################################################
>>>>>>> This message is sent to you because you are subscribed to
>>>>>>> the mailing list Lasso
>>>>>>> [hidden email]
>>>>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>>>>> Send administrative queries to  <[hidden email]>
>>>>>>    #############################################################
>>>>>> This message is sent to you because you are subscribed to
>>>>>> the mailing list Lasso
>>>>>> [hidden email]
>>>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>>>> Send administrative queries to  <[hidden email]>
>>>>>    #############################################################
>>>>> This message is sent to you because you are subscribed to
>>>>> the mailing list Lasso
>>>>> [hidden email]
>>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>>> Send administrative queries to  <[hidden email]>
>>>>   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
>>>>-- -- -- --
>>>> Steve Piercy               Web Site Builder               Soquel, CA
>>>> <[hidden email]>                  <http://www.StevePiercy.com/>
>>>>    #############################################################
>>>> This message is sent to you because you are subscribed to
>>>> the mailing list Lasso
>>>> [hidden email]
>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>> Send administrative queries to  <[hidden email]>
>>>    #############################################################
>>> This message is sent to you because you are subscribed to
>>>  the mailing list Lasso
>>> [hidden email]
>>> To unsubscribe, E-mail to: <[hidden email]>
>>> Send administrative queries to  <[hidden email]>
>>
>>-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
>>Steve Piercy               Web Site Builder               Soquel, CA
>><[hidden email]>                  <http://www.StevePiercy.com/>
>>
>>
>>#############################################################
>>This message is sent to you because you are subscribed to
>>the mailing list Lasso
>>[hidden email]
>>To unsubscribe, E-mail to: <[hidden email]>
>>Send administrative queries to  <[hidden email]>
>
>
>#############################################################
>This message is sent to you because you are subscribed to
>the mailing list Lasso
>[hidden email]
>To unsubscribe, E-mail to: <[hidden email]>
>Send administrative queries to  <[hidden email]>

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- --
Steve Piercy               Web Site Builder              
Soquel, CA
<[hidden email]>                  <http://www.StevePiercy.com/>


#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Best way to compute this average?

Patrick Larkin-2
Well, it looks slightly different.  There are more fields being being pulled in the inline than just the 9 values to be averaged. That's why I was inserting those into an array.  I'm pretty confused on the concept here.  Sorry.  


[

inline(
-inlinename='retrieve_staff_eval_record',
-database='staffEvals',
-table='Eval_Dropin',
-clientusername=$dbu,
-clientpassword=$dbp,
-search,
'id'=var('theRecordID'),
);

        var('teacher_empid')= field('empid');
        var('teacher_name')= field('ln') + ', ' field('fn');
        var('teacher_school_no')= field('school');
        var('teacher_school_name')= field('school_name');
        var('teacher_position')= field('position') + ' (' +field('contract') +')';

/inline;

]

…HTML…

[

local('sum' = 0, 'count' = 0);
local('f' = array('value1','value2','value3','value4','value5','value6','value7','value8','value9'));

records(-InlineName='retrieve_staff_eval_record');
iterate(#f, local('i'));
       
 if(field(#i) != '');
  #sum+=integer(field(#i));
 
        #count+=1;
 /if;
/iterate;
/records;

if(#count > 0);
// prevent divide by zero error
'The correct computation would be ' +#sum + '/' + #count +
' or ';
(decimal(#sum)/decimal(#count))->setformat(-precision=2)&;
else;
'cannot average zero elements';
/if;

#sum;
#count;

]
       



On Dec 2, 2013, at 10:11 AM, Steve Piercy - Web Site Builder <[hidden email]> wrote:

> Let's see the code.  It should look like this:
>
> [
> inline(...moose...);
>    local('f' = array('value1','value2','value3','value4','value5','value6','value7','value8','value9'));
>
>    local('sum' = 0,
>        'count' = 0);
>    records;
>        iterate(#f, local('i'));
>            if(field(#i) != '');
>                #sum+=integer(field(#i));
>                #count+=1;
>            /if;
>        /iterate;
>    /records;
> /inline;
> if(#count > 0);
>    // prevent divide by zero error
>    'The correct computation would be ' +#sum + '/' + #count + ' or ';
>    (decimal(#sum)/decimal(#count))->setformat(-precision=2)&;
> else;
>    'cannot average zero elements';
> /if;
> ]
>
> --steve
>
> On 12/2/13 at 10:06 AM, [hidden email] (Patrick Larkin) pronounced:
>
>> Still same outcome.  Sum and count are 0.
>> I'll try and figure something out.  Thanks.
>>
>>
>> On Dec 2, 2013, at 9:45 AM, Steve Piercy - Web Site Builder <[hidden email]> wrote:
>>
>>> Don't put the record in an array.  Do this:
>>>
>>> records;
>>> iterate(#f, local('i'));
>>> if(field(#i) != '');
>>> #sum+=integer(field(#i));
>>> #count+=1;
>>> /if;
>>> /iterate;
>>> /records;
>>>
>>> --steve
>>>
>>>
>>> On 12/2/13 at 9:38 AM, [hidden email] (Patrick Larkin) pronounced:
>>>
>>>> Hello -
>>>>  Thanks for this response.  I appreciate it.  I'm having a little trouble  understanding this part:
>>>>
>>>>> iterate(#f, local('i'));
>>>>>  if(field(#i) != '');
>>>>>      #sum+=integer(field(#i));
>>>>>      #count+=1;
>>>>>  /if;
>>>>> /iterate;
>>>>   I retrieved a record, placed it in an array (f) and am able to echo the array so
>> I
>>>> know it's contents are correct.  The above code references "field" inside the  iterate.  When I use this, both the sum and count remain 0.   I've never
>> referenced
>>>> an array with "field" before.  Am I doing something wrong or is there something  additional required?    Patrick Larkin
>>>> Information Systems
>>>> Bethlehem Area School District
>>>> https://www.beth.k12.pa.us
>>>>     On Nov 27, 2013, at 4:32 PM, Steve Piercy - Web Site Builder
>> <[hidden email]>
>>>> wrote:
>>>>
>>>>> This version iterates over an array of field names, casts values to integers
>> (in
>>>> 8, all fields are returned as type string and must be cast to their appropriate  type, whereas in 9 fields are returned as their type in the database schema most
>> of
>>>> the time), keeps a sum and a count of integer values, and avoids a divide by
>> zero
>>>> error.
>>>>>  [
>>>>> local('f' =
>>>> array('value1','value2','value3','value4','value5','value6','value7','value8','
>>>> value9'));
>>>>> //local('f' = array('1','','3','0','1','2','','0','3')); // toggle comment for
>>>> testing
>>>>>  local('sum' = 0,
>>>>>  'count' = 0);
>>>>>  iterate(#f, local('i'));
>>>>>  if(field(#i) != '');
>>>>>      #sum+=integer(field(#i));
>>>>>  /*
>>>>> // toggle comment for testing
>>>>>  if(#i != '');
>>>>>      #sum+=integer(#i);
>>>>> */
>>>>>      #count+=1;
>>>>>  /if;
>>>>> /iterate;
>>>>>  if(#count > 0);
>>>>>  // prevent divide by zero error
>>>>>  'The correct computation would be ' +#sum + '/' + #count + ' or ';
>>>>>  (decimal(#sum)/decimal(#count))->setformat(-precision=2)&;
>>>>> else;
>>>>>  'cannot average zero elements';
>>>>> /if;
>>>>> ]
>>>>>  --steve
>>>>>   On 11/27/13 at 11:58 AM, [hidden email] (Tim Taplin) pronounced:
>>>>>
>>>>>> Not really short, but assuming that your values stored in one record, as you
>>>> mentioned, are available as fields, this should work. Likely you were doing  something like this and wished for a shorter method. I was trying to think of a  query or sql method to shorten this but not having any luck at the moment. For  example, it likely would be possible to use an sql statement that could inspect
>> the
>>>> listed fields and return a custom field with the count of nonempty columns as
>> well
>>>> as a field with the sum of those columns. This might wind up being more tedious  than the below code.
>>>>>>   local('value_array' = array());
>>>>>> field('value1') != '' ? #value_array->insert(field('value1'));
>>>>>> field('value2') != '' ? #value_array->insert(field('value2'));
>>>>>> field('value3') != '' ? #value_array->insert(field('value3'));
>>>>>> field('value4') != '' ? #value_array->insert(field('value4'));
>>>>>> field('value5') != '' ? #value_array->insert(field('value5'));
>>>>>> field('value6') != '' ? #value_array->insert(field('value6'));
>>>>>> field('value7') != '' ? #value_array->insert(field('value7'));
>>>>>> field('value8') != '' ? #value_array->insert(field('value8'));
>>>>>> field('value9') != '' ? #value_array->insert(field('value9'));
>>>>>>  local('sum_values' = 0);
>>>>>> loop(#value_array->size);
>>>>>> #sum_values+=#value_array->get(loop_count);
>>>>>> /loop;
>>>>>> local('value_average' = #sum_values/#value_array->size);
>>>>>>  On Nov 27, 2013, at 11:01 AM, Patrick Larkin <[hidden email]> wrote:
>>>>>>
>>>>>>> Sorry, Lasso 8.5.
>>>>>>>     On Nov 27, 2013, at 12:33 PM, Brad Lindsay <[hidden email]> wrote:
>>>>>>>
>>>>>>>> If you're using Lasso 9, query expression:
>>>>>>>> local(myArray) = (:1,'',3,0,1,2,null,0,3)
>>>>>>>> with value in #myArray
>>>>>>>> where string(#value) != ''
>>>>>>>> average decimal(#value)
>>>>>>>> Adjust the where clause as necessary.
>>>>>>>> Brad
>>>>>>>> On 11/27/13, 12:17 PM, Patrick Larkin wrote:
>>>>>>>>> I have to compute an average based on 9 fields that either have a value
>> from
>>>> 0-3 or nothing.  If
>>>>>> the field contains nothing (not zero), it needs to be removed from the average
>>>> computation.  For instance:
>>>>>>>>> value1 = 1
>>>>>>>>> value2 =
>>>>>>>>> value3 = 3
>>>>>>>>> value4 = 0
>>>>>>>>> value5 = 1
>>>>>>>>> value6 = 2
>>>>>>>>> value7 =
>>>>>>>>> value8 = 0
>>>>>>>>> value9 = 3
>>>>>>>>> The correct computation would be `10/7 or 1.43
>>>>>>>>> All of these values would be stored in a single record.  I'd need to test
>>>> for emptiness, calculate
>>>>>> a sum, and a divisor.  Is there a short way anyone can think of to do this?
>>>>>>>>>  Patrick Larkin
>>>>>>>>> Information Systems
>>>>>>>>> Bethlehem Area School District
>>>>>>>>> https://www.beth.k12.pa.us
>>>>>>>>>    #############################################################
>>>>>>>>> This message is sent to you because you are subscribed to
>>>>>>>>> the mailing list Lasso
>>>>>>>>> [hidden email]
>>>>>>>>> To unsubscribe, E-mail to:<[hidden email]>
>>>>>>>>> Send administrative queries to<[hidden email]>
>>>>>>>> #############################################################
>>>>>>>> This message is sent to you because you are subscribed to
>>>>>>>> the mailing list Lasso
>>>>>>>> [hidden email]
>>>>>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>>>>>> Send administrative queries to  <[hidden email]>
>>>>>>>   #############################################################
>>>>>>> This message is sent to you because you are subscribed to
>>>>>>> the mailing list Lasso
>>>>>>> [hidden email]
>>>>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>>>>> Send administrative queries to  <[hidden email]>
>>>>>>   #############################################################
>>>>>> This message is sent to you because you are subscribed to
>>>>>> the mailing list Lasso
>>>>>> [hidden email]
>>>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>>>> Send administrative queries to  <[hidden email]>
>>>>>  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
>>>>> Steve Piercy               Web Site Builder               Soquel, CA
>>>>> <[hidden email]>                  <http://www.StevePiercy.com/>
>>>>>   #############################################################
>>>>> This message is sent to you because you are subscribed to
>>>>> the mailing list Lasso
>>>>> [hidden email]
>>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>>> Send administrative queries to  <[hidden email]>
>>>>   #############################################################
>>>> This message is sent to you because you are subscribed to
>>>> the mailing list Lasso
>>>> [hidden email]
>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>> Send administrative queries to  <[hidden email]>
>>>
>>> -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
>>> Steve Piercy               Web Site Builder               Soquel, CA
>>> <[hidden email]>                  <http://www.StevePiercy.com/>
>>>
>>>
>>> #############################################################
>>> This message is sent to you because you are subscribed to
>>> the mailing list Lasso
>>> [hidden email]
>>> To unsubscribe, E-mail to: <[hidden email]>
>>> Send administrative queries to  <[hidden email]>
>>
>>
>> #############################################################
>> This message is sent to you because you are subscribed to
>> the mailing list Lasso
>> [hidden email]
>> To unsubscribe, E-mail to: <[hidden email]>
>> Send administrative queries to  <[hidden email]>
>
> -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
> Steve Piercy               Web Site Builder               Soquel, CA
> <[hidden email]>                  <http://www.StevePiercy.com/>
>
>
> #############################################################
> This message is sent to you because you are subscribed to
> the mailing list Lasso
> [hidden email]
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>


#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Best way to compute this average?

stevepiercy
That should return something, at least the output from this.

    if(#count > 0);
    The correct computation would be ' +#sum + '/' + #count +
    ' or ';
    (decimal(#sum)/decimal(#count))->setformat(-precision=2)&;
    else;
    'cannot average zero elements';
    /if;

You get *nothing* from that?

Next try some debugging.

    iterate(#f, local('i'));
        #i;
        ' = ';
        field(#i)
        '<br>';
        if(field(#i) != '');

--steve



On 12/2/13 at 10:34 AM, [hidden email] (Patrick Larkin) pronounced:

> Well, it looks slightly different.  There are more fields being being pulled in the
> inline than just the 9 values to be averaged. That's why I was inserting those into
> an array.  I'm pretty confused on the concept here.  Sorry.  
>
>
> [
>
> inline(
> -inlinename='retrieve_staff_eval_record',
> -database='staffEvals',
> -table='Eval_Dropin',
> -clientusername=$dbu,
> -clientpassword=$dbp,
> -search,
> 'id'=var('theRecordID'),
> );
>
>   var('teacher_empid')= field('empid');
>   var('teacher_name')= field('ln') + ', ' field('fn');
>   var('teacher_school_no')= field('school');
>   var('teacher_school_name')= field('school_name');
>   var('teacher_position')= field('position') + ' (' +field('contract') +')';
>
> /inline;
>
> ]
>
> …HTML…
>
> [
>
> local('sum' = 0, 'count' = 0);
> local('f' =
> array('value1','value2','value3','value4','value5','value6','value7','value8','
> value9'));
>
> records(-InlineName='retrieve_staff_eval_record');
> iterate(#f, local('i'));
>  
>  if(field(#i) != '');
>   #sum+=integer(field(#i));
>  
>   #count+=1;
>  /if;
> /iterate;
> /records;
>
> if(#count > 0);
> // prevent divide by zero error
> 'The correct computation would be ' +#sum + '/' + #count +
> ' or ';
> (decimal(#sum)/decimal(#count))->setformat(-precision=2)&;
> else;
> 'cannot average zero elements';
> /if;
>
> #sum;
> #count;
>
> ]
>  
>
>
>
> On Dec 2, 2013, at 10:11 AM, Steve Piercy - Web Site Builder <[hidden email]>
> wrote:
>
> > Let's see the code.  It should look like this:
> >
> > [
> > inline(...moose...);
> >    local('f' =
> array('value1','value2','value3','value4','value5','value6','value7','value8','
> value9'));
> >
> >    local('sum' = 0,
> >        'count' = 0);
> >    records;
> >        iterate(#f, local('i'));
> >            if(field(#i) != '');
> >                #sum+=integer(field(#i));
> >                #count+=1;
> >            /if;
> >        /iterate;
> >    /records;
> > /inline;
> > if(#count > 0);
> >    // prevent divide by zero error
> >    'The correct computation would be ' +#sum + '/' + #count + ' or ';
> >    (decimal(#sum)/decimal(#count))->setformat(-precision=2)&;
> > else;
> >    'cannot average zero elements';
> > /if;
> > ]
> >
> > --steve
> >
> > On 12/2/13 at 10:06 AM, [hidden email] (Patrick Larkin) pronounced:
> >
> >> Still same outcome.  Sum and count are 0.
> >> I'll try and figure something out.  Thanks.
> >>
> >>
> >> On Dec 2, 2013, at 9:45 AM, Steve Piercy - Web Site Builder
> <[hidden email]> wrote:
> >>
> >>> Don't put the record in an array.  Do this:
> >>>
> >>> records;
> >>> iterate(#f, local('i'));
> >>> if(field(#i) != '');
> >>> #sum+=integer(field(#i));
> >>> #count+=1;
> >>> /if;
> >>> /iterate;
> >>> /records;
> >>>
> >>> --steve
> >>>
> >>>
> >>> On 12/2/13 at 9:38 AM, [hidden email] (Patrick Larkin) pronounced:
> >>>
> >>>> Hello -
> >>>>  Thanks for this response.  I appreciate it.  I'm having a little trouble  
> understanding this part:
> >>>>
> >>>>> iterate(#f, local('i'));
> >>>>>  if(field(#i) != '');
> >>>>>      #sum+=integer(field(#i));
> >>>>>      #count+=1;
> >>>>>  /if;
> >>>>> /iterate;
> >>>>   I retrieved a record, placed it in an array (f) and am able to echo the
> array so
> >> I
> >>>> know it's contents are correct.  The above code references "field" inside the  
> iterate.  When I use this, both the sum and count remain 0.   I've never
> >> referenced
> >>>> an array with "field" before.  Am I doing something wrong or is there
> something  additional required?    Patrick Larkin
> >>>> Information Systems
> >>>> Bethlehem Area School District
> >>>> https://www.beth.k12.pa.us
> >>>>     On Nov 27, 2013, at 4:32 PM, Steve Piercy - Web Site Builder
> >> <[hidden email]>
> >>>> wrote:
> >>>>
> >>>>> This version iterates over an array of field names, casts values to integers
> >> (in
> >>>> 8, all fields are returned as type string and must be cast to their
> appropriate  type, whereas in 9 fields are returned as their type in the database
> schema most
> >> of
> >>>> the time), keeps a sum and a count of integer values, and avoids a divide by
> >> zero
> >>>> error.
> >>>>>  [
> >>>>> local('f' =
> >>>>
> array('value1','value2','value3','value4','value5','value6','value7','value8','
> >>>> value9'));
> >>>>> //local('f' = array('1','','3','0','1','2','','0','3')); // toggle comment
> for
> >>>> testing
> >>>>>  local('sum' = 0,
> >>>>>  'count' = 0);
> >>>>>  iterate(#f, local('i'));
> >>>>>  if(field(#i) != '');
> >>>>>      #sum+=integer(field(#i));
> >>>>>  /*
> >>>>> // toggle comment for testing
> >>>>>  if(#i != '');
> >>>>>      #sum+=integer(#i);
> >>>>> */
> >>>>>      #count+=1;
> >>>>>  /if;
> >>>>> /iterate;
> >>>>>  if(#count > 0);
> >>>>>  // prevent divide by zero error
> >>>>>  'The correct computation would be ' +#sum + '/' + #count + ' or ';
> >>>>>  (decimal(#sum)/decimal(#count))->setformat(-precision=2)&;
> >>>>> else;
> >>>>>  'cannot average zero elements';
> >>>>> /if;
> >>>>> ]
> >>>>>  --steve
> >>>>>   On 11/27/13 at 11:58 AM, [hidden email] (Tim Taplin)
> pronounced:
> >>>>>
> >>>>>> Not really short, but assuming that your values stored in one record, as you
> >>>> mentioned, are available as fields, this should work. Likely you were doing  
> something like this and wished for a shorter method. I was trying to think of a  
> query or sql method to shorten this but not having any luck at the moment. For  
> example, it likely would be possible to use an sql statement that could inspect
> >> the
> >>>> listed fields and return a custom field with the count of nonempty columns as
> >> well
> >>>> as a field with the sum of those columns. This might wind up being more
> tedious  than the below code.
> >>>>>>   local('value_array' = array());
> >>>>>> field('value1') != '' ? #value_array->insert(field('value1'));
> >>>>>> field('value2') != '' ? #value_array->insert(field('value2'));
> >>>>>> field('value3') != '' ? #value_array->insert(field('value3'));
> >>>>>> field('value4') != '' ? #value_array->insert(field('value4'));
> >>>>>> field('value5') != '' ? #value_array->insert(field('value5'));
> >>>>>> field('value6') != '' ? #value_array->insert(field('value6'));
> >>>>>> field('value7') != '' ? #value_array->insert(field('value7'));
> >>>>>> field('value8') != '' ? #value_array->insert(field('value8'));
> >>>>>> field('value9') != '' ? #value_array->insert(field('value9'));
> >>>>>>  local('sum_values' = 0);
> >>>>>> loop(#value_array->size);
> >>>>>> #sum_values+=#value_array->get(loop_count);
> >>>>>> /loop;
> >>>>>> local('value_average' = #sum_values/#value_array->size);
> >>>>>>  On Nov 27, 2013, at 11:01 AM, Patrick Larkin <[hidden email]>
> wrote:
> >>>>>>
> >>>>>>> Sorry, Lasso 8.5.
> >>>>>>>     On Nov 27, 2013, at 12:33 PM, Brad Lindsay <[hidden email]> wrote:
> >>>>>>>
> >>>>>>>> If you're using Lasso 9, query expression:
> >>>>>>>> local(myArray) = (:1,'',3,0,1,2,null,0,3)
> >>>>>>>> with value in #myArray
> >>>>>>>> where string(#value) != ''
> >>>>>>>> average decimal(#value)
> >>>>>>>> Adjust the where clause as necessary.
> >>>>>>>> Brad
> >>>>>>>> On 11/27/13, 12:17 PM, Patrick Larkin wrote:
> >>>>>>>>> I have to compute an average based on 9 fields that either have a value
> >> from
> >>>> 0-3 or nothing.  If
> >>>>>> the field contains nothing (not zero), it needs to be removed from the
> average
> >>>> computation.  For instance:
> >>>>>>>>> value1 = 1
> >>>>>>>>> value2 =
> >>>>>>>>> value3 = 3
> >>>>>>>>> value4 = 0
> >>>>>>>>> value5 = 1
> >>>>>>>>> value6 = 2
> >>>>>>>>> value7 =
> >>>>>>>>> value8 = 0
> >>>>>>>>> value9 = 3
> >>>>>>>>> The correct computation would be `10/7 or 1.43
> >>>>>>>>> All of these values would be stored in a single record.  I'd need to test
> >>>> for emptiness, calculate
> >>>>>> a sum, and a divisor.  Is there a short way anyone can think of to do this?
> >>>>>>>>>  Patrick Larkin
> >>>>>>>>> Information Systems
> >>>>>>>>> Bethlehem Area School District
> >>>>>>>>> https://www.beth.k12.pa.us
> >>>>>>>>>    #############################################################
> >>>>>>>>> This message is sent to you because you are subscribed to
> >>>>>>>>> the mailing list Lasso
> >>>>>>>>> [hidden email]
> >>>>>>>>> To unsubscribe, E-mail to:<[hidden email]>
> >>>>>>>>> Send administrative queries to<[hidden email]>
> >>>>>>>> #############################################################
> >>>>>>>> This message is sent to you because you are subscribed to
> >>>>>>>> the mailing list Lasso
> >>>>>>>> [hidden email]
> >>>>>>>> To unsubscribe, E-mail to: <[hidden email]>
> >>>>>>>> Send administrative queries to  <[hidden email]>
> >>>>>>>   #############################################################
> >>>>>>> This message is sent to you because you are subscribed to
> >>>>>>> the mailing list Lasso
> >>>>>>> [hidden email]
> >>>>>>> To unsubscribe, E-mail to: <[hidden email]>
> >>>>>>> Send administrative queries to  <[hidden email]>
> >>>>>>   #############################################################
> >>>>>> This message is sent to you because you are subscribed to
> >>>>>> the mailing list Lasso
> >>>>>> [hidden email]
> >>>>>> To unsubscribe, E-mail to: <[hidden email]>
> >>>>>> Send administrative queries to  <[hidden email]>
> >>>>>  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
> >>>>> Steve Piercy               Web Site Builder               Soquel, CA
> >>>>> <[hidden email]>                  <http://www.StevePiercy.com/>
> >>>>>   #############################################################
> >>>>> This message is sent to you because you are subscribed to
> >>>>> the mailing list Lasso
> >>>>> [hidden email]
> >>>>> To unsubscribe, E-mail to: <[hidden email]>
> >>>>> Send administrative queries to  <[hidden email]>
> >>>>   #############################################################
> >>>> This message is sent to you because you are subscribed to
> >>>> the mailing list Lasso
> >>>> [hidden email]
> >>>> To unsubscribe, E-mail to: <[hidden email]>
> >>>> Send administrative queries to  <[hidden email]>
> >>>
> >>> -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
> >>> Steve Piercy               Web Site Builder               Soquel, CA
> >>> <[hidden email]>                  <http://www.StevePiercy.com/>
> >>>
> >>>
> >>> #############################################################
> >>> This message is sent to you because you are subscribed to
> >>> the mailing list Lasso
> >>> [hidden email]
> >>> To unsubscribe, E-mail to: <[hidden email]>
> >>> Send administrative queries to  <[hidden email]>
> >>
> >>
> >> #############################################################
> >> This message is sent to you because you are subscribed to
> >> the mailing list Lasso
> >> [hidden email]
> >> To unsubscribe, E-mail to: <[hidden email]>
> >> Send administrative queries to  <[hidden email]>
> >
> > -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
> > Steve Piercy               Web Site Builder               Soquel, CA
> > <[hidden email]>                  <http://www.StevePiercy.com/>
> >
> >
> > #############################################################
> > This message is sent to you because you are subscribed to
> > the mailing list Lasso
> > [hidden email]
> > To unsubscribe, E-mail to: <[hidden email]>
> > Send administrative queries to  <[hidden email]>
>
>
> #############################################################
> This message is sent to you because you are subscribed to
>   the mailing list Lasso
> [hidden email]
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Steve Piercy               Web Site Builder               Soquel, CA
<[hidden email]>                  <http://www.StevePiercy.com/>


#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: Best way to compute this average?

Patrick Larkin-2
Steve -

I resolved the issue.  Stupid mistake on my end.  Thanks for your help on this.

Patrick



On Dec 2, 2013, at 11:24 AM, Steve Piercy - Web Site Builder <[hidden email]> wrote:

> That should return something, at least the output from this.
>
>    if(#count > 0);
>    The correct computation would be ' +#sum + '/' + #count +
>    ' or ';
>    (decimal(#sum)/decimal(#count))->setformat(-precision=2)&;
>    else;
>    'cannot average zero elements';
>    /if;
>
> You get *nothing* from that?
>
> Next try some debugging.
>
>    iterate(#f, local('i'));
>        #i;
>        ' = ';
>        field(#i)
>        '<br>';
>        if(field(#i) != '');
>
> --steve
>
>
>
> On 12/2/13 at 10:34 AM, [hidden email] (Patrick Larkin) pronounced:
>
>> Well, it looks slightly different.  There are more fields being being pulled in the
>> inline than just the 9 values to be averaged. That's why I was inserting those into
>> an array.  I'm pretty confused on the concept here.  Sorry.  
>>
>>
>> [
>>
>> inline(
>> -inlinename='retrieve_staff_eval_record',
>> -database='staffEvals',
>> -table='Eval_Dropin',
>> -clientusername=$dbu,
>> -clientpassword=$dbp,
>> -search,
>> 'id'=var('theRecordID'),
>> );
>>
>>  var('teacher_empid')= field('empid');
>>  var('teacher_name')= field('ln') + ', ' field('fn');
>>  var('teacher_school_no')= field('school');
>>  var('teacher_school_name')= field('school_name');
>>  var('teacher_position')= field('position') + ' (' +field('contract') +')';
>>
>> /inline;
>>
>> ]
>>
>> …HTML…
>>
>> [
>>
>> local('sum' = 0, 'count' = 0);
>> local('f' =
>> array('value1','value2','value3','value4','value5','value6','value7','value8','
>> value9'));
>>
>> records(-InlineName='retrieve_staff_eval_record');
>> iterate(#f, local('i'));
>>
>> if(field(#i) != '');
>>  #sum+=integer(field(#i));
>>
>>  #count+=1;
>> /if;
>> /iterate;
>> /records;
>>
>> if(#count > 0);
>> // prevent divide by zero error
>> 'The correct computation would be ' +#sum + '/' + #count +
>> ' or ';
>> (decimal(#sum)/decimal(#count))->setformat(-precision=2)&;
>> else;
>> 'cannot average zero elements';
>> /if;
>>
>> #sum;
>> #count;
>>
>> ]
>>
>>
>>
>>
>> On Dec 2, 2013, at 10:11 AM, Steve Piercy - Web Site Builder <[hidden email]>
>> wrote:
>>
>>> Let's see the code.  It should look like this:
>>>
>>> [
>>> inline(...moose...);
>>>   local('f' =
>> array('value1','value2','value3','value4','value5','value6','value7','value8','
>> value9'));
>>>
>>>   local('sum' = 0,
>>>       'count' = 0);
>>>   records;
>>>       iterate(#f, local('i'));
>>>           if(field(#i) != '');
>>>               #sum+=integer(field(#i));
>>>               #count+=1;
>>>           /if;
>>>       /iterate;
>>>   /records;
>>> /inline;
>>> if(#count > 0);
>>>   // prevent divide by zero error
>>>   'The correct computation would be ' +#sum + '/' + #count + ' or ';
>>>   (decimal(#sum)/decimal(#count))->setformat(-precision=2)&;
>>> else;
>>>   'cannot average zero elements';
>>> /if;
>>> ]
>>>
>>> --steve
>>>
>>> On 12/2/13 at 10:06 AM, [hidden email] (Patrick Larkin) pronounced:
>>>
>>>> Still same outcome.  Sum and count are 0.
>>>> I'll try and figure something out.  Thanks.
>>>>
>>>>
>>>> On Dec 2, 2013, at 9:45 AM, Steve Piercy - Web Site Builder
>> <[hidden email]> wrote:
>>>>
>>>>> Don't put the record in an array.  Do this:
>>>>>
>>>>> records;
>>>>> iterate(#f, local('i'));
>>>>> if(field(#i) != '');
>>>>> #sum+=integer(field(#i));
>>>>> #count+=1;
>>>>> /if;
>>>>> /iterate;
>>>>> /records;
>>>>>
>>>>> --steve
>>>>>
>>>>>
>>>>> On 12/2/13 at 9:38 AM, [hidden email] (Patrick Larkin) pronounced:
>>>>>
>>>>>> Hello -
>>>>>> Thanks for this response.  I appreciate it.  I'm having a little trouble  
>> understanding this part:
>>>>>>
>>>>>>> iterate(#f, local('i'));
>>>>>>> if(field(#i) != '');
>>>>>>>     #sum+=integer(field(#i));
>>>>>>>     #count+=1;
>>>>>>> /if;
>>>>>>> /iterate;
>>>>>>  I retrieved a record, placed it in an array (f) and am able to echo the
>> array so
>>>> I
>>>>>> know it's contents are correct.  The above code references "field" inside the  
>> iterate.  When I use this, both the sum and count remain 0.   I've never
>>>> referenced
>>>>>> an array with "field" before.  Am I doing something wrong or is there
>> something  additional required?    Patrick Larkin
>>>>>> Information Systems
>>>>>> Bethlehem Area School District
>>>>>> https://www.beth.k12.pa.us
>>>>>>    On Nov 27, 2013, at 4:32 PM, Steve Piercy - Web Site Builder
>>>> <[hidden email]>
>>>>>> wrote:
>>>>>>
>>>>>>> This version iterates over an array of field names, casts values to integers
>>>> (in
>>>>>> 8, all fields are returned as type string and must be cast to their
>> appropriate  type, whereas in 9 fields are returned as their type in the database
>> schema most
>>>> of
>>>>>> the time), keeps a sum and a count of integer values, and avoids a divide by
>>>> zero
>>>>>> error.
>>>>>>> [
>>>>>>> local('f' =
>>>>>>
>> array('value1','value2','value3','value4','value5','value6','value7','value8','
>>>>>> value9'));
>>>>>>> //local('f' = array('1','','3','0','1','2','','0','3')); // toggle comment
>> for
>>>>>> testing
>>>>>>> local('sum' = 0,
>>>>>>> 'count' = 0);
>>>>>>> iterate(#f, local('i'));
>>>>>>> if(field(#i) != '');
>>>>>>>     #sum+=integer(field(#i));
>>>>>>> /*
>>>>>>> // toggle comment for testing
>>>>>>> if(#i != '');
>>>>>>>     #sum+=integer(#i);
>>>>>>> */
>>>>>>>     #count+=1;
>>>>>>> /if;
>>>>>>> /iterate;
>>>>>>> if(#count > 0);
>>>>>>> // prevent divide by zero error
>>>>>>> 'The correct computation would be ' +#sum + '/' + #count + ' or ';
>>>>>>> (decimal(#sum)/decimal(#count))->setformat(-precision=2)&;
>>>>>>> else;
>>>>>>> 'cannot average zero elements';
>>>>>>> /if;
>>>>>>> ]
>>>>>>> --steve
>>>>>>>  On 11/27/13 at 11:58 AM, [hidden email] (Tim Taplin)
>> pronounced:
>>>>>>>
>>>>>>>> Not really short, but assuming that your values stored in one record, as you
>>>>>> mentioned, are available as fields, this should work. Likely you were doing  
>> something like this and wished for a shorter method. I was trying to think of a  
>> query or sql method to shorten this but not having any luck at the moment. For  
>> example, it likely would be possible to use an sql statement that could inspect
>>>> the
>>>>>> listed fields and return a custom field with the count of nonempty columns as
>>>> well
>>>>>> as a field with the sum of those columns. This might wind up being more
>> tedious  than the below code.
>>>>>>>>  local('value_array' = array());
>>>>>>>> field('value1') != '' ? #value_array->insert(field('value1'));
>>>>>>>> field('value2') != '' ? #value_array->insert(field('value2'));
>>>>>>>> field('value3') != '' ? #value_array->insert(field('value3'));
>>>>>>>> field('value4') != '' ? #value_array->insert(field('value4'));
>>>>>>>> field('value5') != '' ? #value_array->insert(field('value5'));
>>>>>>>> field('value6') != '' ? #value_array->insert(field('value6'));
>>>>>>>> field('value7') != '' ? #value_array->insert(field('value7'));
>>>>>>>> field('value8') != '' ? #value_array->insert(field('value8'));
>>>>>>>> field('value9') != '' ? #value_array->insert(field('value9'));
>>>>>>>> local('sum_values' = 0);
>>>>>>>> loop(#value_array->size);
>>>>>>>> #sum_values+=#value_array->get(loop_count);
>>>>>>>> /loop;
>>>>>>>> local('value_average' = #sum_values/#value_array->size);
>>>>>>>> On Nov 27, 2013, at 11:01 AM, Patrick Larkin <[hidden email]>
>> wrote:
>>>>>>>>
>>>>>>>>> Sorry, Lasso 8.5.
>>>>>>>>>    On Nov 27, 2013, at 12:33 PM, Brad Lindsay <[hidden email]> wrote:
>>>>>>>>>
>>>>>>>>>> If you're using Lasso 9, query expression:
>>>>>>>>>> local(myArray) = (:1,'',3,0,1,2,null,0,3)
>>>>>>>>>> with value in #myArray
>>>>>>>>>> where string(#value) != ''
>>>>>>>>>> average decimal(#value)
>>>>>>>>>> Adjust the where clause as necessary.
>>>>>>>>>> Brad
>>>>>>>>>> On 11/27/13, 12:17 PM, Patrick Larkin wrote:
>>>>>>>>>>> I have to compute an average based on 9 fields that either have a value
>>>> from
>>>>>> 0-3 or nothing.  If
>>>>>>>> the field contains nothing (not zero), it needs to be removed from the
>> average
>>>>>> computation.  For instance:
>>>>>>>>>>> value1 = 1
>>>>>>>>>>> value2 =
>>>>>>>>>>> value3 = 3
>>>>>>>>>>> value4 = 0
>>>>>>>>>>> value5 = 1
>>>>>>>>>>> value6 = 2
>>>>>>>>>>> value7 =
>>>>>>>>>>> value8 = 0
>>>>>>>>>>> value9 = 3
>>>>>>>>>>> The correct computation would be `10/7 or 1.43
>>>>>>>>>>> All of these values would be stored in a single record.  I'd need to test
>>>>>> for emptiness, calculate
>>>>>>>> a sum, and a divisor.  Is there a short way anyone can think of to do this?
>>>>>>>>>>> Patrick Larkin
>>>>>>>>>>> Information Systems
>>>>>>>>>>> Bethlehem Area School District
>>>>>>>>>>> https://www.beth.k12.pa.us
>>>>>>>>>>>   #############################################################
>>>>>>>>>>> This message is sent to you because you are subscribed to
>>>>>>>>>>> the mailing list Lasso
>>>>>>>>>>> [hidden email]
>>>>>>>>>>> To unsubscribe, E-mail to:<[hidden email]>
>>>>>>>>>>> Send administrative queries to<[hidden email]>
>>>>>>>>>> #############################################################
>>>>>>>>>> This message is sent to you because you are subscribed to
>>>>>>>>>> the mailing list Lasso
>>>>>>>>>> [hidden email]
>>>>>>>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>>>>>>>> Send administrative queries to  <[hidden email]>
>>>>>>>>>  #############################################################
>>>>>>>>> This message is sent to you because you are subscribed to
>>>>>>>>> the mailing list Lasso
>>>>>>>>> [hidden email]
>>>>>>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>>>>>>> Send administrative queries to  <[hidden email]>
>>>>>>>>  #############################################################
>>>>>>>> This message is sent to you because you are subscribed to
>>>>>>>> the mailing list Lasso
>>>>>>>> [hidden email]
>>>>>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>>>>>> Send administrative queries to  <[hidden email]>
>>>>>>> -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
>>>>>>> Steve Piercy               Web Site Builder               Soquel, CA
>>>>>>> <[hidden email]>                  <http://www.StevePiercy.com/>
>>>>>>>  #############################################################
>>>>>>> This message is sent to you because you are subscribed to
>>>>>>> the mailing list Lasso
>>>>>>> [hidden email]
>>>>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>>>>> Send administrative queries to  <[hidden email]>
>>>>>>  #############################################################
>>>>>> This message is sent to you because you are subscribed to
>>>>>> the mailing list Lasso
>>>>>> [hidden email]
>>>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>>>> Send administrative queries to  <[hidden email]>
>>>>>
>>>>> -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
>>>>> Steve Piercy               Web Site Builder               Soquel, CA
>>>>> <[hidden email]>                  <http://www.StevePiercy.com/>
>>>>>
>>>>>
>>>>> #############################################################
>>>>> This message is sent to you because you are subscribed to
>>>>> the mailing list Lasso
>>>>> [hidden email]
>>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>>> Send administrative queries to  <[hidden email]>
>>>>
>>>>
>>>> #############################################################
>>>> This message is sent to you because you are subscribed to
>>>> the mailing list Lasso
>>>> [hidden email]
>>>> To unsubscribe, E-mail to: <[hidden email]>
>>>> Send administrative queries to  <[hidden email]>
>>>
>>> -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
>>> Steve Piercy               Web Site Builder               Soquel, CA
>>> <[hidden email]>                  <http://www.StevePiercy.com/>
>>>
>>>
>>> #############################################################
>>> This message is sent to you because you are subscribed to
>>> the mailing list Lasso
>>> [hidden email]
>>> To unsubscribe, E-mail to: <[hidden email]>
>>> Send administrative queries to  <[hidden email]>
>>
>>
>> #############################################################
>> This message is sent to you because you are subscribed to
>>  the mailing list Lasso
>> [hidden email]
>> To unsubscribe, E-mail to: <[hidden email]>
>> Send administrative queries to  <[hidden email]>
>
> -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
> Steve Piercy               Web Site Builder               Soquel, CA
> <[hidden email]>                  <http://www.StevePiercy.com/>
>
>
> #############################################################
> This message is sent to you because you are subscribed to
>  the mailing list Lasso
> [hidden email]
> To unsubscribe, E-mail to: <[hidden email]>
> Send administrative queries to  <[hidden email]>


#############################################################
This message is sent to you because you are subscribed to
  the mailing list Lasso
[hidden email]
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>