Counting number of tabs in a variable

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
warrenk
Posts: 110
Joined: Sun Sep 21, 2008 5:39 am

Counting number of tabs in a variable

Post by warrenk » 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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Fri Apr 10, 2009 9:04 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Post by FourthWorld » Fri Apr 10, 2009 9:19 pm

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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Fri Apr 10, 2009 9:29 pm

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

warrenk
Posts: 110
Joined: Sun Sep 21, 2008 5:39 am

Post by warrenk » Fri Apr 10, 2009 10:10 pm

Richard/Bernd,

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

Have a great weekend!
Warren

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Fri Apr 10, 2009 11:53 pm

Bernd:

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

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Sat Apr 11, 2009 10:07 am

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Post by FourthWorld » Sun Apr 12, 2009 1:00 am

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply