Page 1 of 1

Find what word a character is in

Posted: Wed Oct 15, 2014 10:53 am
by andrewferguson
Hi,

I need a way to find out what word a certain character is in. For example, say I had char 4 of "LiveCode Forums", the result would be 1 (because char 4 is in the first word of "LiveCode Forums".

I looked in the dictionary, and both "charIndex" and "lineIndex" seem very similar to what I am trying to do, but for characters and lines, instead of words. I cannot seem to find a "wordIndex" in the dictionary.

Any ideas?

Thanks,
Andrew

Re: Find what word a character is in

Posted: Wed Oct 15, 2014 11:01 am
by dave.kilroy
Hi Andrew

Check out 'wordOffset' in the dictionary (and while you're there also check out charOffset and lineOffset)

Kind regards

Dave

Re: Find what word a character is in

Posted: Wed Oct 15, 2014 11:25 am
by andrewferguson
Hi Dave,
Thanks for the reply.

Unfortunately "wordOffset" is not quite what I am looking for, because I need char 11 of "LiveCode Forums" to return 2, because the 11th char of "LiveCode Forums" occurs in word 2. If I try that with "wordOffset", the result is 1, because the 11th char of "LiveCode Forums" (which is an "o") also occurs in the first word.

Any ideas?
Thanks,
Andrew

Re: Find what word a character is in

Posted: Wed Oct 15, 2014 11:32 am
by jmburnod
Hi Andrew,

If I understand correctly what you want this function could be useful:

Code: Select all

function offsetLetterInWord pText,pLetter
   put empty into roffsetLetterInWord
   put 0 into tCount
   repeat for each word pWord in pText
      add 1 to tCount
      put pLetter & ";" & tCount & ";" & pWord & ";" & offset(pLetter,pWord) & ";"  & cr after roffsetLetterInWord
   end repeat
   delete char -1 of roffsetLetterInWord
   filter roffsetLetterInWord without "*0;"
   return roffsetLetterInWord
end offsetLetterInWord
Best regards
Jean-Marc

Re: Find what word a character is in

Posted: Wed Oct 15, 2014 11:39 am
by andrewferguson
Hi Jean-Marc,
Thanks, but I cannot seem to get your function to work. When I try

Code: Select all

put offsetLetterInWord("LiveCode Forums", 11)
the result is empty, and I need it to be 2

Andrew

Re: Find what word a character is in

Posted: Wed Oct 15, 2014 12:01 pm
by bn
Hi Andrew,

try something like this

Code: Select all

 put "LiveCode Forums" into tData
put the number of words of (char 1 to 11 of tData) 
of course you can elaborate on that.

I don't understand if you are using this repeatedly or only in a one-off fashion.

Kind regards
Bernd

Re: Find what word a character is in

Posted: Wed Oct 15, 2014 12:41 pm
by jmburnod
Andrew,

pLetter is a letter.
You can do like that:

Code: Select all

put offsetLetterInWord("LiveCode Forums", char 11 of "LiveCode Forums")
Best regards
Jean-Marc

Re: Find what word a character is in

Posted: Wed Oct 15, 2014 1:03 pm
by andrewferguson
Hi,
Thanks, Bernd's solution was just what I was looking for!
Andrew

Re: Find what word a character is in

Posted: Wed Oct 15, 2014 1:10 pm
by dave.kilroy
Andrew

Both Jean-Marc's and Bernd's approaches work fine - indeed both of them have helped people here countless times...

Code: Select all

get numberoftimeshelped("Jean-Marc") --results in %^&*^%$£
get numberoftimeshelped("bernd") --results in £$%^&*^%£@£~
Dave