Find offset of first and last char of Word x in a field

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 508
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Find offset of first and last char of Word x in a field

Post by kaveh1000 » Sun Mar 24, 2019 12:09 pm

This probably has a simple answer but I can't find it!

I just need to find the offset of first and last char of say Word 5 in a field. How can I do that?

Kaveh
Kaveh

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Find offset of first and last char of Word x in a field

Post by jmburnod » Sun Mar 24, 2019 1:07 pm

Hi Kaveh,
Here is a way, tested with "word" not tested with "trueword"

Code: Select all

...
   get word 1 to  pNumW-1 of fld 1
   put the num of chars of it +2 into tNumFirstC
   put  tNumFirstC + (the num of chars of word pNumW of fld 1) into  tNumLastC
   put tNumFirstC & "," & tNumLastC-1
...
Best
Jean-Marc
https://alternatic.ch

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Find offset of first and last char of Word x in a field

Post by Thierry » Sun Mar 24, 2019 1:17 pm

Hi Kaveh,

As you are a fan of regex, here is my take on this:

Code: Select all

function forKaveh T
   local Rslt
   local Rx = "(?msx) (?:\w+?\s){4} (\w+) "
   get matchChunk( T, Rx, p0, p1)
   put "Indexes: " & p0 & " - " & p1 & cr into Rslt
   // only for testing:
   put char p0 to p1 of T after Rslt
   return Rslt   
end forKaveh

and to use this function:

Code: Select all

on mouseUp
   local inputText
   put line -8 to -2 of the script of me into inputText
    
   answer forKaveh( inputText) &cr& word 5 of inputText
   
end mouseUp

/*

This probably has a simple answer but I can't find it!

I just need to find the offset
of first and last char of say Word 5 in a field.
How can I do that?

Kaveh
*/

and a bit more generic:

Code: Select all

function forKaveh T, n
   local Rslt
   local Rx = "(?msx) (?:\w+?\s){__N__} (\w+) "
   replace "__N__" with ( n - 1) in Rx
   get matchChunk( T, Rx, p0, p1)
   ....
Warning: this works for plain words and
needs some adjustments for trueWords or whatever they are.
consider this as an exercice for the reader...

Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 508
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Find offset of first and last char of Word x in a field

Post by kaveh1000 » Sun Mar 24, 2019 2:39 pm

Thank you both.

I opted for J-M's method as it is to extend to truewords. (I failed to mention truewords was what I really needed.) Thierry's solution is in my regex library to study, especially (?msx), which I have not used.

It is probably not useful to put my code here as it is very specific, but basically the same method as Jean-Marc suggested. The only correction factor is that with truewords the first word will typically not start at char 1, so that needs to be dealt with.

Thank you both again.

Regards
Kaveh
Kaveh

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1209
Joined: Thu Apr 11, 2013 11:27 am

Re: Find offset of first and last char of Word x in a field

Post by LCMark » Mon Mar 25, 2019 10:47 am

@kaveh1000: I think you are looking for the 'charIndex' property. e.g. Given a field (1) containing "Hello World Again!":

Code: Select all

put the charIndex of char 1 of word 2 of field 1 -> 7
put the charIndex of char -1 of word 2 of field 1 -> 11
This should work for any chunk - so trueWord too.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Find offset of first and last char of Word x in a field

Post by jmburnod » Mon Mar 25, 2019 11:22 am

Hi,
Thanks Mark
"charindex" way works fine with "word" and "trueword" :D
https://alternatic.ch

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Find offset of first and last char of Word x in a field

Post by richmond62 » Mon Mar 25, 2019 11:39 am

Code: Select all

on mouseUp
   put fld "fORIGIN" into TEKST
   put word 5 of TEKST into W5
   put char 1 of W5 into C1
   put the last char of W5 into C2
   put W5 && "is the 5th word in your text, and the first letter is" && C1 & ", and the last letter is" && C2 into fld "fRESULT"
   set the textStyle of word 1 of fld "fRESULT" to bold
   set the textColor of word 1 of fld "fRESULT" to red
   set the textStyle of word 14 of fld "fRESULT" to bold
   set the textColor of word 14 of fld "fRESULT" to red
   set the textStyle of word 20 of fld "fRESULT" to bold
   set the textColor of word 20 of fld "fRESULT" to red
end mouseUp
-
word5.png
Attachments
Word 5.livecode.zip
Here's the stack.
(929 Bytes) Downloaded 233 times

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 508
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Find offset of first and last char of Word x in a field

Post by kaveh1000 » Mon Mar 25, 2019 2:30 pm

Bingo!!

LC and LCMark do it again.

Yup, both these work correctly:

Code: Select all

  put char (charindex of trueword 4 of fld 1) of fld 1
  put char (charindex of word 4 of fld 1) of fld 1
Thanks again Mark and others. Every answer adds to my knowledge and perhaps others' too...

Kaveh
Kaveh

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Find offset of first and last char of Word x in a field

Post by jmburnod » Mon Mar 25, 2019 5:58 pm

Hi,
Using "Charindex" is defititively better than my first post which doesn't work if you have double space.
Jean-Marc
https://alternatic.ch

Post Reply

Return to “Talking LiveCode”