Not displaying the percentage.

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
Michaellad
Posts: 12
Joined: Tue Nov 15, 2016 12:17 pm

Not displaying the percentage.

Post by Michaellad » Fri Nov 18, 2016 3:23 pm

Code: Select all

on mouseUp
   initialise myText, arrayname, arraymark, arraypercentage, student
   getfile myText
   readfile student, arrayname, arraymark, arraypercentage
end mouseUp

on initialise @myText, @student, @arrayname, @arraymark, @arraypercentage
   put "" into myText
   put 0 into student
   put "" into arrayname
   put "" into arraymark
   put 0 into arraypercentage 
end initialise
 
on getfile @text
   answer file "Please choose a file to read into your stack."
   if the result is not "cancel" THEN
      put it into myText
      put URL ("file:" & myText) into field "output1"
      end if
end getfile

on readfile  student, arrayname, arraymark, arraypercentage
   local  maximum
   local position
   local loop
   put 0 into arraypercentage
   put 0 into maximum
   put 0 into position
   put 50 into student
   
   repeat with loop = 1 to the number of lines of myText
      put line loop of myText into student
      split student by comma
      put student[1] into arrayname[loop]
      put student[2] into arrayfullname
      put student[3] into arraymark[loop]
      
      put  arrayname[loop] & tab & arraymark[loop] & tab & arraypercentage[loop] into line 1  of field "output1"
   end repeat
         
   put arraymark[1] into maximum
   
   repeat with loop = 2 to the number of lines of myText
      if arraymark[loop] > maximum then 
         put arraymark[loop] into maximum 
         put loop into position 
      end if
   end repeat

   set numberformat to "0" 
   put "The student with the highest percentage is" & arrayname[position] & "with percentage of" & arraymark[loop] &" %" into line 2 of  fld"output2"
end readfile


 

My code is not displaying the percentage or the name of the highest mark.

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

Re: Not displaying the percentage.

Post by dunbarx » Fri Nov 18, 2016 5:27 pm

I would say that myText is empty. 8)

Try moving the local commands to the top of the script. Know that when you declare either local or global variables, they MUST reside ABOVE all handlers that need them. I bet this is your problem, since you seem more than able to drive LC.

Craig Newman

Klaus
Posts: 14192
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Not displaying the percentage.

Post by Klaus » Fri Nov 18, 2016 6:59 pm

Hi Michael,

what Craig said! :D

Hint: myText will only hold the FILENAME of the file the user has selected and not its content!

And please check your earlier postings, the proposals (guesses to be honest) are already pile up here:
http://forums.livecode.com/viewtopic.php?f=7&t=28315


Best

Klaus

Michaellad
Posts: 12
Joined: Tue Nov 15, 2016 12:17 pm

Re: Not displaying the percentage.

Post by Michaellad » Fri Nov 18, 2016 11:02 pm

Thanks for the help, I will try what you have told me when I get the chance.

Michaella

Michaellad
Posts: 12
Joined: Tue Nov 15, 2016 12:17 pm

Re: Not displaying the percentage.

Post by Michaellad » Tue Nov 22, 2016 12:20 pm

How can I put the file into myText?

Klaus
Posts: 14192
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Not displaying the percentage.

Post by Klaus » Tue Nov 22, 2016 1:34 pm

Michaellad wrote:How can I put the file into myText?
The same way as you put it into your field "output1"! 8)

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Not displaying the percentage.

Post by MaxV » Tue Nov 22, 2016 2:48 pm

Michaellad wrote:...

My code is not displaying the percentage or the name of the highest mark.
Hi,
your code is not very livecode style, this is probably what you need:

########CODE#######
on mouseUp
getfile
readfile
end mouseUp

on getfile
answer file "Please choose a file to read into your stack."
if the result is not "cancel" THEN
put it into myText
put URL ("file:" & myText) into field "output1"
end if
end getfile

on readfile
put field "output1" into myText#getFile message already sent the text into field output1
put 0 into maximum
set itemdel to comma
repeat for each line myRow in myText
#array students is: [name][surname]["sum_of_marks"]["number_of_marks"]
add item 3 of myRow to students[(item 1 of myRow)] [(item 2 of myRow)]["sum_of_Marks"] # sum of the marks
add 1 to students[item 1 of myRow][item 2 of myRow]["Number_of_Marks"] # number of the marks
end repeat
#now we calculate percentages
repeat for each key tName in students
repeat for each key tSurname in students[tName]
put students[tName][tSurname]["sum_of_marks"] / students[tName][tSurname]["number_of_Marks"] into tperc
if tperc > maximum then
put tName & space & tSurname into winner
put tperc into maximum
end if
end repeat
end repeat
answer "The student with the highest percentage is " & winner & " with percentage of " & round(maximum)
end readfile
#####END OF CODE#####
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Post Reply