count field if..

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
alberto.fre
Posts: 20
Joined: Sat Nov 16, 2013 12:12 pm

count field if..

Post by alberto.fre » Sat Aug 09, 2014 10:37 am

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

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: count field if..

Post by jmburnod » Sat Aug 09, 2014 10:55 am

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
https://alternatic.ch

alberto.fre
Posts: 20
Joined: Sat Nov 16, 2013 12:12 pm

Re: count field if..

Post by alberto.fre » Sat Aug 09, 2014 1:36 pm

thanks, it works fine; but to just get the number of empty fields of a group?

sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

Re: count field if..

Post by sefrojones » Sat Aug 09, 2014 1:57 pm

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

alberto.fre
Posts: 20
Joined: Sat Nov 16, 2013 12:12 pm

Re: count field if..

Post by alberto.fre » Sat Aug 09, 2014 2:37 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: count field if..

Post by dunbarx » Sat Aug 09, 2014 2:40 pm

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

alberto.fre
Posts: 20
Joined: Sat Nov 16, 2013 12:12 pm

Re: count field if..

Post by alberto.fre » Sat Aug 09, 2014 3:07 pm

thank you, really simple and functional. My code is prehistoric :oops: . I have to study a lot more

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: count field if..

Post by dunbarx » Sat Aug 09, 2014 3:11 pm

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

alberto.fre
Posts: 20
Joined: Sat Nov 16, 2013 12:12 pm

Re: count field if..

Post by alberto.fre » Sat Aug 09, 2014 3:15 pm

encouraged :idea:
thank's

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: count field if..

Post by FourthWorld » Sat Aug 09, 2014 4:05 pm

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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply