Page 1 of 1

Coursework

Posted: Wed Nov 30, 2016 3:36 pm
by Michaellad
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

Re: Coursework

Posted: Wed Nov 30, 2016 4:05 pm
by richmond62
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 199 times

Re: Coursework

Posted: Wed Nov 30, 2016 4:22 pm
by dunbarx
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

Re: Coursework

Posted: Wed Nov 30, 2016 4:25 pm
by richmond62
Why not just do this
Because it's not half as much fun on St. Andrews' day :)

Re: Coursework

Posted: Wed Nov 30, 2016 4:41 pm
by shaosean
error in the code above, it should be

Code: Select all

add 1 to addWS[tChar]

Re: Coursework

Posted: Wed Nov 30, 2016 6:34 pm
by dunbarx
Sean.

Fixed in the original. Thanks.

Craig

Re: Coursework

Posted: Wed Nov 30, 2016 6:37 pm
by dunbarx
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

Re: Coursework

Posted: Wed Nov 30, 2016 6:41 pm
by richmond62
set the caseSensitive to true

Re: Coursework

Posted: Wed Nov 30, 2016 8:34 pm
by dunbarx
CaseSensitive.

Much better. Of course.

Craig

Re: Coursework

Posted: Fri Dec 02, 2016 2:55 pm
by Michaellad
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

Re: Coursework

Posted: Fri Dec 02, 2016 3:17 pm
by shaosean
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.. ;)

Re: Coursework

Posted: Fri Dec 02, 2016 3:21 pm
by Michaellad

Code: Select all

 display_answer buyerinfo, buyername, buyersurname, numbertickets

Re: Coursework

Posted: Fri Dec 02, 2016 3:33 pm
by Klaus
You handler "display_answer" has SIX parameter, but you are only supplying FOUR in your script!
Maybe that is the problem?

Re: Coursework

Posted: Fri Dec 02, 2016 3:35 pm
by Michaellad
Thank you, it is working now.

Re: Coursework

Posted: Fri Dec 02, 2016 5:53 pm
by dunbarx
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