Counting delimiter characters in a string

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
Clarkey
Posts: 109
Joined: Fri Jun 11, 2010 11:10 am

Counting delimiter characters in a string

Post by Clarkey » Sat Jun 26, 2010 5:59 pm

Hi folks, Can some kind soul please point me to the mechanism that lets me count the number of instances of a specific string or character - such as a comma - in a string of text.
Thanks,
Keith..

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

Re: Counting delimiter characters in a string

Post by bn » Sat Jun 26, 2010 6:38 pm

Hi Keith,
you could use the itemDelimiter.
The default is comma.
if you ask for the number of items in tText it returns the number of commas. Although, if the last char is a comma it will return the number of commas -1, so you might want to check for that.
You can set the itemdelimiter to anything you want, it is case sensitive.
regards
Bernd

Clarkey
Posts: 109
Joined: Fri Jun 11, 2010 11:10 am

Re: Counting delimiter characters in a string

Post by Clarkey » Sat Jun 26, 2010 7:10 pm

Hi Bernd,
Many thanks, once again for reframing the question into one that has an obvious answer - count the items not the commas!
Who was it that said it isn't the notes that make the music but the gaps in between. (That was a rhetorical question that you don't need to answer!) :D
Best,
Keith..

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Counting delimiter characters in a string

Post by jmburnod » Sat Jun 26, 2010 9:54 pm

Hi Keith and Bernd,

if i have understand the question, this script would be useful

Code: Select all

function NbOccurInTexte psearch,pCont
   put 0 into rNbOccurInTexte
   put the num of chars of pCont into nbC
   repeat until UneOcc = 0
      put offset(psearch,pCont) into UneOcc
      if UneOcc = 0 then exit repeat
      put char UneOcc+1  to nbc of pCont into pCont
      add 1 to rNbOccurInTexte
      wait 2 milliseconds with messages
   end repeat
  return rNbOccurInTexte
end NbOccurInTexte

Best

Jean-Marc
https://alternatic.ch

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

Re: Counting delimiter characters in a string

Post by bn » Sat Jun 26, 2010 10:35 pm

Salut Jean-Marc,

you could also say

Code: Select all

function NbOccurInTexte psearch,pCont
   put 0 into rNbOccurInTexte
   put 0 into tCharsToSkip
   repeat
      put offset(psearch, pCont, tCharsToSkip) into UneOcc
      if UneOcc = 0 then exit repeat
      add UneOcc to tCharsToSkip
      add 1 to rNbOccurInTexte
   end repeat
   return rNbOccurInTexte
end NbOccurInTexte
I like offset because it is fast and efficient. But in this case Keith was looking for just on character, so I thought why not let Rev do the calculation.
By the way, in this case, where Rev is not doing anything except offset, (no moving of stuff or updating the interface) I would leave out the wait in the repeat loop. In my experience it is for very long repeats or very large dataSets a wait with messages or 0 milliseconds is recommended. Although, if your loop is running away = infinite, then without the wait you have a hard time to stop execution (command period) so it might be prudent to leave it in.
put char UneOcc+1 to nbc of pCont into pCont
you make Rev shuffle memory around, that takes time. Rev knows very well at what char it is and can access that faster then shuffling memory if you use the charsToSkip.
This of course if you work on very large datasets where you like to save all the milliseconds you can.

regards
Bernd

Clarkey
Posts: 109
Joined: Fri Jun 11, 2010 11:10 am

Re: Counting delimiter characters in a string

Post by Clarkey » Sat Jun 26, 2010 10:55 pm

Jean-Marc,
Thanks for the response and the script. Bernd's suggestion of items has solved my initial requirement - to find the commas that I'd added to a revXMLTree extract to identify levels of encapsulation.

On the more general requirement - needing to count the instances of a character or string in another string - I was slightly surprised that there isn't a standard command to 'count the chunks that meet specific criteria within a chunk'.

So thanks for addressing this issue, because I foresee having to do that in the near future, too! :)
Best,
Keith..

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

Re: Counting delimiter characters in a string

Post by bn » Sat Jun 26, 2010 11:48 pm

Keith,
have a look at the user guide pdf, pp 175, section 6.1 Chunk etc. It is well worth to understand how Rev operates with/on chunks. It is a real timesaver and one of the strengths of Rev. You can operate on chunks in chunks. They explain it there. Just note that a word in rev is not always what you think a word is. E.g. wouw! is a word including the exlamation mark.
Unfortunately you can not define a string as a delimiter as you can in Applescript. Only single chars.
regards
Bernd

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Counting delimiter characters in a string

Post by jmburnod » Sun Jun 27, 2010 10:09 am

Salut Bernd,

Your corrections are really very useful for us
The next time i will look at the doc first :?

Vielen dank wieder

Jean-Marc
https://alternatic.ch

Clarkey
Posts: 109
Joined: Fri Jun 11, 2010 11:10 am

Re: Counting delimiter characters in a string

Post by Clarkey » Sun Jun 27, 2010 10:41 am

Bernd,
Thanks for the steer re chunks within chunks.
Best,
Keith..

urbaud
Posts: 120
Joined: Tue Feb 24, 2009 12:10 am

Re: Counting delimiter characters in a string

Post by urbaud » Sun Sep 12, 2010 4:42 am

Hi Bernd,

I was reading your comments in the post dated Jun 26, 2010, "Counting delimiter characters in a string" where you suggest that Keith review the user guide, p 175, section 6.1. Just to let you know, and I guess others, that I tried the following: add 1 to word 3 of field "Numbers", as outlined in the guide and shown below. I got the error message cited below each time I tried it.

This is the script I used:
on mouseUp
add 1 to word 3 of field "Numbers"
end mouseUp

6.1.2 Using Chunks with Containers (User Guide 4.0)
You can use a chunk of a container anywhere you use an entire container. For example, you can use the add command to add a number to a line of a field:

add 1 to word 3 of field "Numbers"

You can also use chunk expressions to replace (using the put command) or remove (using the deletecommand) any portion of a container.

ERROR: "execution error at line 2 (add: destination has a bad format (numeric?)), char 1"

What do you make of this?

Dan
urbaud

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

Re: Counting delimiter characters in a string

Post by Mark » Sun Sep 12, 2010 8:43 am

Dan,

Try this:

Code: Select all

on mouseUp
  if word 3 of fld "Numbers" is a number then
    add 1 to word 3 of field "Numbers"
  else
    beep
    answer error "Word 3 of field Numbers (" & word 3 of fld "Numbers" & ") is not a number!"
  end if
end mouseUp
What happens?

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

Regulae
Posts: 136
Joined: Tue Oct 20, 2009 6:05 am

Re: Counting delimiter characters in a string

Post by Regulae » Sun Sep 12, 2010 9:29 am

Hi all,
I hope Bernd won’t mind my jumping in here, and he may well have something interesting to add, but I found:

Code: Select all

on mouseUp
   add 1 to word 3 of field "Numbers"
end mouseUp
... in a button does work correctly, provided word 3 of field “Numbers” is a number, e.g. if field “Numbers” contains:
1 2 3 4
... each mouseUp will add 1 to the third word, i.e.:
1 2 4 4
1 2 5 4
1 2 6 4
... and so on.
It’s worth keeping in mind, as Bernd mentioned, Rev’s definition of a “word”, as the dictionary says:
A word is delimited by one or more spaces, tabs, or returns, or enclosed by double quotes.
... so 1234 and “1 2 3 4” are each considered one word (remembering that “everything in quotes is a single word” has tripped me up on occasion). Back to our example, if (word 3 of field “Numbers”) is not a number, trying to add 1 to it generates the error. An interesting case is when there are less than 3 words in field “Numbers”, for example, if field “Numbers” is empty, successive “add 1 to word 3 of field “Numbers”” generates:
111111111111...
Edit: I see that Mark is thinking very much along the same lines.
Regards,
Michael

urbaud
Posts: 120
Joined: Tue Feb 24, 2009 12:10 am

Re: Counting delimiter characters in a string

Post by urbaud » Sun Sep 12, 2010 10:45 pm

Hi Mark and Regulae,
Thank you both for your comments. Regulae, you're right if fld "Numbers" actually contains numbers and are separated by spaces, etc. When I put this post up I thought it (add 1 to word 3 of field "Numbers") might just be a typo and did not try putting digits into the "Numbers" fld. Also, I forgot that a number (integer) separated by spaces is 'seen' by Rev as a "word", but also as a character that can be part of a calculation.

And Mark, your code uses the 'number' function to have Rev actually evaluate whether the 3rd word is an integer. If it isn't, then it uses the Answer command to tell the user that it isn't a number and of course places it in parentheses. The guide assumes that the reader (newbies like myself) know or remember what a "word" is according to Rev. In this instance I didn't.

By the way, I copied your line: answer error "Word 3 of field Numbers (" & word 3 of fld "Numbers" & ") is not a number!" into my own data base for future reference. Getting the syntax correct for a line of code for me is sometimes difficult, so when I find one that I haven't seen before and it works, I copy it. Then it's just an easy matter to search the data base for the correct wording for a particular command or function.
urbaud

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

Re: Counting delimiter characters in a string

Post by Mark » Mon Sep 13, 2010 8:33 am

Hi Urbaud,

So this is working well for you? The documentation very often gives very simple examples, which you need to put into context. The docs seem to assume some kind of ideal world, but very often you need to check that controls exist, that files are readable, and that variables contain the right type of data. This is necessary with every programming language, but even more so with Revolution, because of its flexible nature.

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

Post Reply