Find what word a character is in
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- VIP Livecode Opensource Backer
- Posts: 184
- Joined: Wed Apr 10, 2013 5:09 pm
Find what word a character is in
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
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
-
- VIP Livecode Opensource Backer
- Posts: 858
- Joined: Wed Jun 24, 2009 1:17 pm
- Contact:
Re: Find what word a character is in
Hi Andrew
Check out 'wordOffset' in the dictionary (and while you're there also check out charOffset and lineOffset)
Kind regards
Dave
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..."
-
- VIP Livecode Opensource Backer
- Posts: 184
- Joined: Wed Apr 10, 2013 5:09 pm
Re: Find what word a character is in
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
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
Hi Andrew,
If I understand correctly what you want this function could be useful:
Best regards
Jean-Marc
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
Jean-Marc
https://alternatic.ch
-
- VIP Livecode Opensource Backer
- Posts: 184
- Joined: Wed Apr 10, 2013 5:09 pm
Re: Find what word a character is in
Hi Jean-Marc,
Thanks, but I cannot seem to get your function to work. When I trythe result is empty, and I need it to be 2
Andrew
Thanks, but I cannot seem to get your function to work. When I try
Code: Select all
put offsetLetterInWord("LiveCode Forums", 11)
Andrew
Re: Find what word a character is in
Hi Andrew,
try something like this
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
try something like this
Code: Select all
put "LiveCode Forums" into tData
put the number of words of (char 1 to 11 of tData)
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
Andrew,
pLetter is a letter.
You can do like that:
Best regards
Jean-Marc
pLetter is a letter.
You can do like that:
Code: Select all
put offsetLetterInWord("LiveCode Forums", char 11 of "LiveCode Forums")
Jean-Marc
https://alternatic.ch
-
- VIP Livecode Opensource Backer
- Posts: 184
- Joined: Wed Apr 10, 2013 5:09 pm
Re: Find what word a character is in
Hi,
Thanks, Bernd's solution was just what I was looking for!
Andrew
Thanks, Bernd's solution was just what I was looking for!
Andrew
-
- VIP Livecode Opensource Backer
- Posts: 858
- Joined: Wed Jun 24, 2009 1:17 pm
- Contact:
Re: Find what word a character is in
Andrew
Both Jean-Marc's and Bernd's approaches work fine - indeed both of them have helped people here countless times...
Dave
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 £$%^&*^%£@£~
"...this is not the code you are looking for..."