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.

question & exclaimation marks

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
NewzNZ
Halfop
Posts: 68
Joined: Thu Mar 05, 2009 5:15 am
Contact:

question & exclaimation marks

Post by NewzNZ »

Hi there

Am just trying to filter question marks or exclaimation marks in a string...just wondering if something like this is the right way about it:

Code: Select all

	if {[string match {*\?*} $var] || [string match {*\!*} $var]} {
         do some stuff here etc
}
Thanks in advance for any help!
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

What do you mean by filtering? If you mean to see if the string has any of the two then you got two options: string index or regexp.

string first

Code: Select all

% set text "something ! in here"
something ! in here
% string first "!" $text
10
notice that this returns the position of the needle in the haystack, so you need to do something like:

Code: Select all

if {[string first "!" $text] > -1 || [string first "?" $text] > -1} {
# do something
}
regexp:

Code: Select all

% regexp -- {\!|\?} $text
1
notice that this one returns 0/1 (false or true) if any of the two is in there.

If you meant to replace/remove them from the string then you need string map like this:

Code: Select all

% string map {"!" ""} $text
something  in here
% set text "something ? in here"
something ? in here
% string map {"?" ""} $text
something  in here
Edit: Fixed a typo.
Last edited by caesar on Mon Aug 24, 2020 12:38 am, edited 1 time in total.
Once the game is over, the king and the pawn go back in the same box.
User avatar
NewzNZ
Halfop
Posts: 68
Joined: Thu Mar 05, 2009 5:15 am
Contact:

Post by NewzNZ »

Hi - that's great thank you for the suggestions - will try those options...

Thank you again!
Post Reply