Coursework

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

Coursework

Post by Michaellad » Wed Nov 30, 2016 3:36 pm

I need to add up all of the "s" and "W" in a file, how can i do this?

this is what ive done so far.

Code: Select all

on mouseUp
   initialise 
   read_in MyText
   display_answer buyerinfo, buyername, buyersurname, numbertickets
   findingsales arraym
end mouseUp

on initialise 
   put "" into MyText
   put "" into buyerinfo
   put "" into buyername
   put "" into buyersurname
   put 0 into numbertickets
   put 0 into total
end initialise

on read_in @MyText
   Answer file "Please choose a file to read into your LiveCode stack"
   
   if the result is not "cancel" then
      put it into MyText 
      put url("file:" & MyText) into MyText
      end if
   end read_in
   
   on display_answer MyText, @buyerinfo, @buyername, @buyersurname, @numbertickets, @total
      local loop
      repeat with loop = 1 to number of lines of MyText
         put line loop of MyText into buyerinfo
         split buyerinfo by comma
         
         
         put buyerinfo[1] into buyername[loop] 
         put buyerinfo[2] into buyersurname[loop]
         put buyerinfo[3] into numbertickets[loop]
         
         
         put buyername[loop] & tab & buyersurname[loop] & tab & numbertickets[loop]  into line loop of field "output2"
      end repeat
      local tickets, totalamount, maxtickets
      put 0 into tickets
      put 10 into maxtickets
      put numbetickets[1] into totalamount
      
      repeat with loop = 2 to number of lines of MyText 
         if numbertickets[loop] > totalamount then 
            put numbertickets[loop] into totalamount 
            put loop into tickets
            end if
         end repeat
         
         set numberformat to "0" 
         
         
      end display_answer
      
      on findingsales
         local scounter, wcounter
         put 0 into scounter
         put 0 into wcounter
         repeat with loop = 1 to number of lines of MyText
            if arraym[loop] = "S"
            THEN put +1 into scounter
           then arraym[loop] = "w" 
             put +1 into wcounter
           end if
                     end repeat
      end findingsales

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10096
Joined: Fri Feb 19, 2010 10:17 am

Re: Coursework

Post by richmond62 » Wed Nov 30, 2016 4:05 pm

I need to add up all of the "s" and "W" in a file
If ALL you need to do is count up incidences of "s" and "W" in a file I cannot quite understand why you
have just long, complicated code . . .

Having read your file into a field [e.g. field "textToLookAt"] I would just "trot" through it a bit like this:

on mouseUp
put fld "textToLookAt" into tTLA
set the caseSensitive to true
repeat until tTLA is empty
put char 1 of tTLA into HOLDER
if HOLDER = "s" then
add 1 to SSS
end if
if HOLDER = "W" then
add 1 to WWW
end if
delete char 1 of tTLA
end repeat
put ("W = " & WWW && "s = " & SSS) into fld "fResult"
end mouseUp
walloper.png
walloper.livecode.zip
Here's the stack.
(1.17 KiB) Downloaded 200 times

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

Re: Coursework

Post by dunbarx » Wed Nov 30, 2016 4:22 pm

Hi.

Why not just do this:

Code: Select all

on mouseUp
   get yourFile
   repeat for each char tChar in it
      add 1 to addWS[tChar]
   end repeat
   breakPoint
end mouseUp
If you check out the array variable "addWS". you will see the counts of those chars.

Craig Newman
Last edited by dunbarx on Wed Nov 30, 2016 6:33 pm, edited 1 time in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10096
Joined: Fri Feb 19, 2010 10:17 am

Re: Coursework

Post by richmond62 » Wed Nov 30, 2016 4:25 pm

Why not just do this
Because it's not half as much fun on St. Andrews' day :)

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: Coursework

Post by shaosean » Wed Nov 30, 2016 4:41 pm

error in the code above, it should be

Code: Select all

add 1 to addWS[tChar]

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

Re: Coursework

Post by dunbarx » Wed Nov 30, 2016 6:34 pm

Sean.

Fixed in the original. Thanks.

Craig

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

Re: Coursework

Post by dunbarx » Wed Nov 30, 2016 6:37 pm

Just noticed in your later example that the chars might be case sensitive.

If so, are you OK with modifying the array example accordingly? All the others need the same loving care, as LC does not as default care at all.

Write back if you never heard of the "charToNum" function. But look it up first.

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10096
Joined: Fri Feb 19, 2010 10:17 am

Re: Coursework

Post by richmond62 » Wed Nov 30, 2016 6:41 pm

set the caseSensitive to true

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

Re: Coursework

Post by dunbarx » Wed Nov 30, 2016 8:34 pm

CaseSensitive.

Much better. Of course.

Craig

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

Re: Coursework

Post by Michaellad » Fri Dec 02, 2016 2:55 pm

It works now but theres a problem with the top of the code.

button "Get amount of tickets": execution error at line 25 (Handler: error in parameter expression) near "display_answer", char 1

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: Coursework

Post by shaosean » Fri Dec 02, 2016 3:17 pm

Can you post the line that is throwing the error.. I'm too lazy to count in the code posted at the top of this thread.. ;)

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

Re: Coursework

Post by Michaellad » Fri Dec 02, 2016 3:21 pm

Code: Select all

 display_answer buyerinfo, buyername, buyersurname, numbertickets

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

Re: Coursework

Post by Klaus » Fri Dec 02, 2016 3:33 pm

You handler "display_answer" has SIX parameter, but you are only supplying FOUR in your script!
Maybe that is the problem?

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

Re: Coursework

Post by Michaellad » Fri Dec 02, 2016 3:35 pm

Thank you, it is working now.

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

Re: Coursework

Post by dunbarx » Fri Dec 02, 2016 5:53 pm

Michael.

Know that the "missing" parameters are still passed even though you never explicitly snared and named them. They exist as elements of the "params" function, and if you have a little spare time, it would probably be a good thing to add a line or two in your unfixed handler to "see" them. Just to get practice.

Oftentimes it is not known how many parameters are passed to another handler, nor are those parameters particularly named in any useful way, and this powerful tool then becomes invaluable.

Craig

Post Reply