Page 1 of 1

Counting delimiter characters in a string

Posted: Sat Jun 26, 2010 5:59 pm
by Clarkey
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..

Re: Counting delimiter characters in a string

Posted: Sat Jun 26, 2010 6:38 pm
by bn
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

Re: Counting delimiter characters in a string

Posted: Sat Jun 26, 2010 7:10 pm
by Clarkey
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..

Re: Counting delimiter characters in a string

Posted: Sat Jun 26, 2010 9:54 pm
by jmburnod
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

Re: Counting delimiter characters in a string

Posted: Sat Jun 26, 2010 10:35 pm
by bn
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

Re: Counting delimiter characters in a string

Posted: Sat Jun 26, 2010 10:55 pm
by Clarkey
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..

Re: Counting delimiter characters in a string

Posted: Sat Jun 26, 2010 11:48 pm
by bn
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

Re: Counting delimiter characters in a string

Posted: Sun Jun 27, 2010 10:09 am
by jmburnod
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

Re: Counting delimiter characters in a string

Posted: Sun Jun 27, 2010 10:41 am
by Clarkey
Bernd,
Thanks for the steer re chunks within chunks.
Best,
Keith..

Re: Counting delimiter characters in a string

Posted: Sun Sep 12, 2010 4:42 am
by urbaud
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

Re: Counting delimiter characters in a string

Posted: Sun Sep 12, 2010 8:43 am
by Mark
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

Re: Counting delimiter characters in a string

Posted: Sun Sep 12, 2010 9:29 am
by Regulae
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

Re: Counting delimiter characters in a string

Posted: Sun Sep 12, 2010 10:45 pm
by urbaud
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.

Re: Counting delimiter characters in a string

Posted: Mon Sep 13, 2010 8:33 am
by Mark
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