Counting number of tabs in a variable
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Counting number of tabs in a variable
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
Hi Warren,
this is simple and effective, Rev being fast on chars in a repeat loop
regards
Bernd
this is simple and effective, Rev being fast on chars in a repeat loop
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
Bernd
-
- VIP Livecode Opensource Backer
- Posts: 10049
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
The beauty (and sometimes frustration) with Rev is that there's almost always more than one way to skin a cat:
Code: Select all
set the itemdel to tab
get the number of items of tMyData
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Richard,
I tried that first. But you have to check whether there is anything behind the last tab or not. If tab is the last char it works, otherwise it reports 1 item to many.
So I figured that the safest way would be to just count the tabs in a repeat loop, especially since it is very fast in the repeat for each form.
regards
Bernd
I tried that first. But you have to check whether there is anything behind the last tab or not. If tab is the last char it works, otherwise it reports 1 item to many.
So I figured that the safest way would be to just count the tabs in a repeat loop, especially since it is very fast in the repeat for each form.
regards
Bernd
Mark,
that works fine.
Richard,
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.
regards
Bernd
the benchmarking code in case I goofed it up again
that works fine.
Richard,
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.
regards
Bernd
the benchmarking code in case I goofed it up again
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
-
- VIP Livecode Opensource Backer
- Posts: 10049
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn