Counting number of tabs in a variable
Posted: Fri Apr 10, 2009 8:40 pm
I am trying to count the number of <tabs> in a variable. What would be the easiest way to do this?
Thanks,
Warren
Thanks,
Warren
Questions and answers about the LiveCode platform.
https://www.forums.livecode.com/
Code: Select all
-- put your data into a variable, here its called temp
put 0 into tCounter
repeat for each char aChar in temp
if aChar = tab then add 1 to tCounter
end repeat
put tCounter
Code: Select all
set the itemdel to tab
get the number of items of tMyData
Code: Select all
on mouseUp pMouseBtnNo
put field 1 into temp
put 0 into tCounter
set cursor to watch
put 1000 into tHowOften
put the millisec into tStart
repeat tHowOften
repeat for each char aChar in temp
if aChar = tab then add 1 to tCounter
end repeat
end repeat
put "repeat " & the millisec - tStart & " msec " & tCounter/tHowOften into field 2
put the millisec into tStart
set the itemdel to tab
repeat tHowOften
put number of items of (temp & "x") - 1 into temp2
end repeat
put return & "item " & the millisec - tStart & "msec " & temp2 after field 2
end mouseUp
Under the hood both algorithms are doing very similar tasks, but using "the number of items" lets all of that happen in highly-optimized C-based code, whereas writing you're own repeat loop for the same result requires another layer of overhead.bn wrote:I did some benchmarking with the 'item' version (with Marks modification) and the 'repeat for each' version on a text with 50 items and each item consiting of 50 chars
I was surprised to see the item version beeing so much faster:
on a MacBook Pro 2.33
doing this a thousand times for both versions:
item: 3 millisecs
repeat 500 millisecs
So its the item way.