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.

Character hiding and showing

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Character hiding and showing

Post by Madalin »

Evening.
I have a small problem with the following code, meaning that it works 97% :)

I cannot make it show
'
`
and
?

Code: Select all

proc hideword {word} {

	set newword "";
	foreach letter [split $word {}] {
		if {$letter ne " " && $letter ne "-" && $letter ne "," && $letter ne "." && $letter ne "(" && $letter ne ")" && $letter ne "'" && $letter ne "`" && $letter ne "?"} {set letter "*";}
		append newword $letter;
	}
	return $newword;
}
User avatar
caesar
Mint Rubber
Posts: 3776
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Have you tried string map like:

Code: Select all

string map [list " " "" "-" "" "," "" "." "" "\(" "" "\)" "" "\'" "" "`" "" "?" "" "*" ""] [split $text]
that basically removes the characters leaving everything else?
Once the game is over, the king and the pawn go back in the same box.
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Post by Madalin »

I want to show does characters not hide them.
User avatar
caesar
Mint Rubber
Posts: 3776
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Ah, sorry, my bad. Loop and lsearch then?

Code: Select all

proc hideword word {
	set chars {" " "-" "," "." "\("  "\)" "\'" "`" "?" "*"}
	foreach c [split $word ""] {
		if {[lsearch $chars $c] > -1} {
			append nw $c
		} else {
			append nw "*"
		}
	}
	return $nw
}
and a "compact" version:

Code: Select all

proc hideword word {
	set chars {" " "-" "," "." "\("  "\)" "\'" "`" "?" "*"}
	foreach c [split $word ""] {
		append nw [expr {[lsearch $chars $c] > -1 ? $c : "*"}]
	}
	return $nw
}
Once the game is over, the king and the pawn go back in the same box.
Post Reply