Help wanted for using "Scan" command
Posted by karthikshenoy (karthikshenoy), 15 June 2003hi everybody!
i am new to tcl/tk. please help me with this problem. i seem to be getting nowhere
i am trying to split a string of this format:
abhor$to hate; to detest
i am using the following scan command:
scan $entireline "%[^$]%*[$]%[^$]" word meaning
what i would like is for word and meaning to have the following values:
word = abhor
meaning = to hate; to detest
what is wrong? how do i get what i want?
thanks in advance!
Posted by admin (Graham Ellis), 15 June 2003Hi, Welcome ...
scan is one of those commands that sometimes I wish didn't exist in Tcl - it's been there since the language first started, but things have moved on and there's nearly always a better way to do things. I've looked at the problem you posed, and if I was writing the code I would use a regular expression via the regexp command - here's an example:
and when I run that, it says:
The word abhor means to hate; to detest
which is what you're looking for.
Lots of other ways to do the same thing - let me know if you need any more detail for the above, or you would like to look at further alternatives
Posted by karthikshenoy (karthikshenoy), 15 June 2003Thanks Graham! This works perfectly.
I have kinda understood the (.*)\\\$(.*) regular expression that you have used but I would like to know more about the \\\$ part.
$ is a token which has another meaning so we need to use a delimiter that is \. But I could not understand why we need to use 3 consecutive \ s.
I am still wondering why the scan function refuses to behave the way it claims to. I have tried various combinations of teh format string but i just cannot achieve accurate results with the scan command.
Really appreciate your help. Thanks!!
Posted by admin (Graham Ellis), 15 June 2003Quote:
Yep ... I've heard that MANY times before. Don't use scan ... by the way, I've learnt enough of scan to avoid it usually, so I'm not a good person to advise on what's wrong with your string.
New subject - the \\\$.
Tcl scripts are interpretted by the language interpresster as the script is run, and the $ character is a special to tell the interpretter that a variable name is starting and its contents must be substituted in. You do NOT want this to happen as your $ really is a $, so you put a \ in front of in to tell the Tcl interpretter that the $ has an alternative meaning - it REALLY IS a $.
But that's not the end of the story. In a regular expression, a $ also has a special meaning ... but once again you really want a $. Solution? Include a \ in the regular expression to tell that to the regular expression handler. To get that \ through the Tcl interpretter, though, you have to preceed it by a \ - thus the third
\ in the regular expression.
Thus ..... in the Tcl source
Which means tha
is passed to the regular expression handler, so it really looks for
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: firstname.lastname@example.org • WEB: http://www.wellho.net • SKYPE: wellho