Delet all characters after word x

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Delet all characters after word x

Post by Nakia » Wed Feb 13, 2013 1:15 am

Hi,

Is there an easy way to delete all characters (including tab,return etc) after a certain word in a line of text.
The amount and content of characters either side of word 6 varies so applying a fixed delete (char 5 to 7 etc) won't work..

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Delet all characters after word x

Post by sturgis » Wed Feb 13, 2013 1:33 am

Use matchchunk to find the match. (you might look at wholematches in the dictionary too)

get matchchunk(whateverDataToSearch,"theWordToFind",tStartPosition,tEndPosition)

If a match is found the special variable - it - will contain true, false if the match fails. So if you have a match, tEndPosition will then contain the char position of the last char of the word that was matched.

To delete everything after that word, just

Code: Select all

delete char tEndPosition to -1 of whateverDataWasUsed -- whateverDataWasUsed is the container with the text you are trying to modify. 

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Delet all characters after word x

Post by Simon » Wed Feb 13, 2013 1:37 am

Ok have lots to assume here.

Code: Select all

put "I am a rather fantastic person who enjoys an afternoon at the pub" into tLine
put word 1 to 6 of tLine into tLine
Is that what you mean?
Maybe:

Code: Select all

repeat for each line tLine in tText
put word 1 to 6 of line tLine & cr after tNewText
end repeat
delete last line of tNewText
put tNewText into tText
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Delet all characters after word x

Post by Nakia » Wed Feb 13, 2013 1:58 am

Thanks Sturgis,

I can use that and get TRUE on "it" but when I answer the tStart and tAnswer variables i get an empty return..
my word to find is a Variable if that matters?

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Delet all characters after word x

Post by sturgis » Wed Feb 13, 2013 2:08 am

if it is ALWAYS word 6, definitely go with simons answer.

If you don't know what word it will be, and you removing ALL data after the word (in a multi line set of text) then matchchunk or even wordoffset should work.

To test, I set up 2 fields and a button, Field 1 has some text.
Field 2 is for the output. \

In the button is the following code

Code: Select all

on mouseUp
   put field 1 into tData --put the data into a variable so its faster
   get matchchunk(tData,"(variable)",tStart,tEnd) -- look for match. Forgot that parens mark the start end end of the match position that will then go into tStart and tEnd
   if it is true then delete char tEnd to -1 of tData -- if there was a match, remove everything to the end
   put tData into field 2 -- put the modified text into field 2
end mouseUp
Using wordoffset is very similar

Code: Select all

on mouseup
  put field 1 into tData
   put  wordOffset("variable",tData) into tOffset -- find the first occurrence 
   if tOffset > 0 then delete word (tOffset + 1) to -1 of tData -- if there was a match, the offset will be > 0 so delete starting at the word AFTER the match, to the end of the text
   put tData into field 2  -- put the modified text into field 2
end mouseup


But as mentioned, if you need to do this for each single line (a line being delimited by return) definitely go with what simon says. *grin*

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Delet all characters after word x

Post by Simon » Wed Feb 13, 2013 3:11 am

oops I made a mistake...

Code: Select all

put word 1 to 6 of line tLine & cr after tNewText
should be

Code: Select all

put word 1 to 6 of tLine & cr after tNewText
But still not sure if I'm answering the question. :)
It did read to me like it was always word 6, but I get these things wrong often.



Simon
VGhpcyBzdGFydGVkIHdpdGggS2V2aW4gb2YgY291cnNlCldobyBzcG9rZSB0aWxsIGhpcyB2
b2ljZSB3YXMgaG9yc2UKV2l0aCBhIHByb21vdGlvbgpXZSdsbCBnZXQgbXVjaCBtb3JlIG1v
dGlvbgpUaGlzIHNvZnR3YXJlIHdpbGwgZ28gb3BlbiBzb3VyY2U=
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Delet all characters after word x

Post by sturgis » Wed Feb 13, 2013 3:52 am

I'm just glad I got to use the phrase "simon says"

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10356
Joined: Wed May 06, 2009 2:28 pm

Re: Delet all characters after word x

Post by dunbarx » Wed Feb 13, 2013 4:06 am

Another county heard from.

I assume it is not always word 6. This will do the trick yet another way, though there will be problems if the word in question occurs in more than one place. Fortunately, there are simple ways around that as well.

Given a field "yourField" with some text in it, put this in a button somewhere:

Code: Select all

on mouseUp
   ask "delete text after which word?"
   find it
   delete char (word 4 of the foundChunk + 1) to -1 of fld "yourField"
end mouseUp
This likely needs firming up, as it makes assumptions about your text similar to the comment above. Write back...

Craig Newman

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Delet all characters after word x

Post by Nakia » Wed Feb 13, 2013 6:33 am

Thanks all,

ended up using Simon's suggestion, not really sure why I didnt look at it from that angle before..

Thanks again..

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7394
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Delet all characters after word x

Post by jacque » Wed Feb 13, 2013 7:13 pm

Just for another perspective, I'd probably do it this way:

Code: Select all

get wordOffset(tWordToFind,tText)
if it > 0 then delete word it+1 to -1 of tText
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply