Page 1 of 1

Counting number of tabs in a variable

Posted: Fri Apr 10, 2009 8:40 pm
by warrenk
I am trying to count the number of <tabs> in a variable. What would be the easiest way to do this?

Thanks,
Warren

Posted: Fri Apr 10, 2009 9:04 pm
by bn
Hi Warren,

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 
regards

Bernd

Posted: Fri Apr 10, 2009 9:19 pm
by FourthWorld
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

Posted: Fri Apr 10, 2009 9:29 pm
by bn
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

Posted: Fri Apr 10, 2009 10:10 pm
by warrenk
Richard/Bernd,

Thanks...this is exactly what I am looking for.

Have a great weekend!
Warren

Posted: Fri Apr 10, 2009 11:53 pm
by Mark
Bernd:

set the itemdel to tab
return number of items of (myVar & "x") - 1

Best,

Mark

Posted: Sat Apr 11, 2009 10:07 am
by bn
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

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

Posted: Sun Apr 12, 2009 1:00 am
by FourthWorld
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.
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.