Page 1 of 1

Calculating number of button clicks and switches

Posted: Thu Mar 20, 2008 5:38 am
by ac11ca
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

Posted: Thu Mar 20, 2008 6:31 am
by Janschenkel
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.

Posted: Sat Mar 22, 2008 2:02 am
by ac11ca
Absolute magic! :D Thank you very much for that coding Jan, it works perfectly.

Cheers,
Adrian