Page 1 of 1

count field if..

Posted: Sat Aug 09, 2014 10:37 am
by alberto.fre
Hi,
what command I can use to count only the fields blank? and what is its syntax? :?
my program calculates the average of seven rates entered by the user, but i also want to calculate the partial average.
thank you

Re: count field if..

Posted: Sat Aug 09, 2014 10:55 am
by jmburnod
Hi Alberto,
what command I can use to count only the fields blank? and what is its syntax?

Code: Select all

This function return a empty field list:

on mouseup
   put getEmptyField() 
end mouseup

function getEmptyField
   put empty into rEmptyField
   put the num of flds into nbF
   repeat with i = 1 to nbF
      if fld i = empty then 
         put the short name of fld i & cr after rEmptyField
      end if
   end repeat
   delete char-1 of rEmptyField
   return rEmptyField
end getEmptyField
Best regards
Jean-Marc

Re: count field if..

Posted: Sat Aug 09, 2014 1:36 pm
by alberto.fre
thanks, it works fine; but to just get the number of empty fields of a group?

Re: count field if..

Posted: Sat Aug 09, 2014 1:57 pm
by sefrojones
Just changed Jean-Marc's function a bit to add a parameter:

Code: Select all

on mouseup
      put getEmptyField("MyGroupName")  
end mouseup

function getEmptyField sGroupID
   put empty into rEmptyField
   put the num of flds of grp  sGroupID into nbF
   repeat with i = 1 to nbF
      if fld i of grp sGroup = empty then 
         put the short name of fld i of  grp sGroupID & cr after rEmptyField
      end if
   end repeat
   delete char-1 of rEmptyField
   return rEmptyField
end getEmptyField

You can also use group name or number with this function.

--Sefro

Re: count field if..

Posted: Sat Aug 09, 2014 2:37 pm
by alberto.fre
Thank you for your attention
my target is to calculate the partial average when the fields aren't all completed
I cite my code for example. This code works fine for my target, but it isn't elegant. I need to count the empty fields to calculate the partial average.

Code: Select all

on mouseUp
   set the numberFormat to "#.00"
   
   put field "ammissione" into tData1
   put field "italiano" into tData2
   put field "matematica" into tData3
   put field "inglese" into tData4
   put field "2a lingua" into tData5
   put field "INVALSI" into tData6
   put field "orale" into tData7
   
   put (tData1 + tData2 + tData3 + tData4 + tData5 + tData6 + tData7) into tDataSum
   
   if tData1 > 0 then
      put 1 into tNum1
   end if
   if tData2 > 0 then
      put 1 into tNum2
   end if
   if tData3 > 0 then
      put 1 into tNum3
   end if
   if tData4 > 0 then
      put 1 into tNum4
   end if
   if tData5 > 0 then
      put 1 into tNum5
   end if
   if tData6 > 0 then
      put 1 into tNum6
   end if
   if tData7 > 0 then
      put 1 into tNum7                  
   end if
   
   
   put (tNum1 + tNum2 + tNum3 + tNum4 + tNum5 + tNum6 + tNum7) into tNumSum

-- it calculates the partial average
   put tDataSum / tNumSum into fld "media"
   
end mouseUp

Re: count field if..

Posted: Sat Aug 09, 2014 2:40 pm
by dunbarx
Hi.

Try this. You should be able to adapt it to your needs. In a new card make several fields. Place a value in some of them, but not all. Make a button, and place this in its script:

Code: Select all

on mouseUp
   repeat with y = 1 to the number of flds
      if fld y > 0 then 
         add 1 to divisor
         add fld y to numerator
         end if
      end repeat
      answer numerator / divisor
end mouseUp
Craig Newman

Re: count field if..

Posted: Sat Aug 09, 2014 3:07 pm
by alberto.fre
thank you, really simple and functional. My code is prehistoric :oops: . I have to study a lot more

Re: count field if..

Posted: Sat Aug 09, 2014 3:11 pm
by dunbarx
Alberto.
My code is prehistoric
The more clunky and verbose your coding, the faster you learn. Think how much more raw LC you used to write your handlers than I did. Think of it as working out in a gym. All that effort becomes part of you.

Craig

Re: count field if..

Posted: Sat Aug 09, 2014 3:15 pm
by alberto.fre
encouraged :idea:
thank's

Re: count field if..

Posted: Sat Aug 09, 2014 4:05 pm
by FourthWorld
dunbarx wrote:The more clunky and verbose your coding, the faster you learn.
"Good judgment comes from experience. Experience comes from bad judgment."
- Mark Twain