This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Take words from a sentence (help)

Old posts that have not been replied to for several years.
Locked
c
cerberus_gr
Halfop
Posts: 97
Joined: Fri Feb 07, 2003 8:57 am
Location: 127.0.0.1

Take words from a sentence (help)

Post by cerberus_gr »

Hello,

which command do we use, if we have a sentence like "word1 word2 word3 word4 word5 word6" and we want
to save to a variable only "word2 word3 word4"? We don't know the number of words, but only which are these words.

Example
sentence 1 -> My name is George and I am 27 years old.
sentence 2 -> My first name is George and I am 27 years old and I live...
need -> George and I am 27

If I want to save to a variable the sentence from George until the end?

What should I do if the sentence has characters such as ", [. { etc.
Should I use join and split command?

thx
User avatar
Dereckson
Voice
Posts: 20
Joined: Thu May 30, 2002 8:00 pm
Location: Belgium
Contact:

Post by Dereckson »

Instead join/split, you need something convert CORRECTLY your string into a list.

This is the solution I use :

Code: Select all

proc splitwords {string} { 
	set return ""
	foreach word [regexp -inline -all -- {\S+} $string] { 
		lappend return $word
	} 
	return $return
}
User avatar
caesar
Mint Rubber
Posts: 3777
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Or see/use the "lrange" here..
eg: set bla [lrange $text 4 end]
Once the game is over, the king and the pawn go back in the same box.
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

The regexp is not needed, join and split are fine.

First you split the string into a list, so you can operate on it in a array like fassion.

One you have finished with it as a list, and need a string again, you use the join command.

In this, treat the incoming text as $text

Code: Select all

#Convert strin to list, and place it back in the same variable
set text [split $text]
#extract using lrange
set part [lrange $text 4 end]
#Now convert $part to a string so the characters do not look garbled.
set part [join $part]
However, this doesn't cover extracting the details you wanted.

Because of the way the 2 sentaces are structured (differently), ity would take some form of AI system, to know what part of the text is what.

If you know of a script, be it in another language, that can do this, paste it here, we can try and convert it to Tcl.
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Post by strikelight »

Your original post is a bit confusing as to what you want exactly..

From your example, if you only wanted 'George and I am 27',
I would recommend using:

Code: Select all

regexp -nocase {name is (.*) years} garbage sentence
And the contents of $sentence would then be 'George and I am 27' from your given example data.

However, you went on to say 'from George until the end'.. If this is the case, I would recommend simply using:

Code: Select all

regexp -nocase {name is (.*)} garbage sentence
And $sentence would then contain from the name until the end of the sentence.

For more information, consult the re_syntax part of TCL Manpages.
Locked