Calculating number of button clicks and switches

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
ac11ca
Posts: 41
Joined: Sun Mar 16, 2008 2:22 am

Calculating number of button clicks and switches

Post by ac11ca » Thu Mar 20, 2008 5:38 am

Hi, I am at a loss as to how the program the following:

I have 2 buttons, let's call them "A" and "B". People using my program will be clicking on these two buttons often and in any order that they like. For example, somone might click on them in this order: A A A B B B B A A A A B B B B B B B B A A A

I would like to know the following things:

1. the number of times they switch a series of clicks from one button to the next. For example, in the series above they switched 4 times.

2. the average number of clicks made in each series of button clicks. For example, in the series above this would be (3+4+4+8+3) / 5 = 4.4

Thanks for any help,
Ac

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Thu Mar 20, 2008 6:31 am

Here's a quick and dirty solution using three fields and three buttons.
1. Field "Switches" without script
2. Field "Average" without script
3. Field "Sequence" with script:

Code: Select all

on RecalcStatistics
  -- init some variables
  put the text of me into tText
  put char 1 of tText into tPrevChar
  put 0 into tSwitchCount
  put 1 into tSequenceLength
  put empty into tSequenceLengthList
  -- now iterate over the text
  repeat with i = 2 to length(tText)
    if char i of tText is not tPrevChar then
      -- we have a switch
      add 1 to tSwitchCount
      put tSequenceLength & comma after tSequenceLengthList
      put 1 into tSequenceLength
      put char i of tText into tPrevChar
    else
      add 1 to tSequenceLength
    end if
  end repeat
  -- don't forget to take the last sequence into account
  add 1 to tSwitchCount
  put tSequenceLength after tSequenceLengthList
  -- update the average and swtich count fields
  put average(tSequenceLengthList) into field "Average"
  put tSwitchCount into field "Switches"
end RecalcStatistics
4. Button "A" with script:

Code: Select all

on mouseUp
  put "A" after field "Sequence"
  send "RecalcStatistics" to field "Sequence"
end mouseUp
5. Button "B" with script:

Code: Select all

on mouseUp
  put "B" after field "Sequence"
  send "RecalcStatistics" to field "Sequence"
end mouseUp
6. Button "Clear" with script:

Code: Select all

on mouseUp
  put empty into field "Sequence"
  send "RecalcStatistics" to field "Sequence"
end mouseUp
Hope this helped,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

ac11ca
Posts: 41
Joined: Sun Mar 16, 2008 2:22 am

Post by ac11ca » Sat Mar 22, 2008 2:02 am

Absolute magic! :D Thank you very much for that coding Jan, it works perfectly.

Cheers,
Adrian

Post Reply