Find what word a character is in

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
andrewferguson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 184
Joined: Wed Apr 10, 2013 5:09 pm

Find what word a character is in

Post by andrewferguson » Wed Oct 15, 2014 10:53 am

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

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: Find what word a character is in

Post by dave.kilroy » Wed Oct 15, 2014 11:01 am

Hi Andrew

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

Kind regards

Dave
"...this is not the code you are looking for..."

andrewferguson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 184
Joined: Wed Apr 10, 2013 5:09 pm

Re: Find what word a character is in

Post by andrewferguson » Wed Oct 15, 2014 11:25 am

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

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

Re: Find what word a character is in

Post by jmburnod » Wed Oct 15, 2014 11:32 am

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
https://alternatic.ch

andrewferguson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 184
Joined: Wed Apr 10, 2013 5:09 pm

Re: Find what word a character is in

Post by andrewferguson » Wed Oct 15, 2014 11:39 am

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

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

Re: Find what word a character is in

Post by bn » Wed Oct 15, 2014 12:01 pm

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

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

Re: Find what word a character is in

Post by jmburnod » Wed Oct 15, 2014 12:41 pm

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
https://alternatic.ch

andrewferguson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 184
Joined: Wed Apr 10, 2013 5:09 pm

Re: Find what word a character is in

Post by andrewferguson » Wed Oct 15, 2014 1:03 pm

Hi,
Thanks, Bernd's solution was just what I was looking for!
Andrew

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: Find what word a character is in

Post by dave.kilroy » Wed Oct 15, 2014 1:10 pm

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
"...this is not the code you are looking for..."

Post Reply