summaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authordkf <donal.k.fellows@manchester.ac.uk>2004-04-22 22:36:19 (GMT)
committerdkf <donal.k.fellows@manchester.ac.uk>2004-04-22 22:36:19 (GMT)
commit1f89d8146b999776f7affe00b2856dc17e580a6a (patch)
tree4b42325bf7114ba0392baeae37859dc607c8b8c2 /doc
parent4a01f0af989c83035aaa4ac6f86f7527927d2240 (diff)
downloadtcl-1f89d8146b999776f7affe00b2856dc17e580a6a.zip
tcl-1f89d8146b999776f7affe00b2856dc17e580a6a.tar.gz
tcl-1f89d8146b999776f7affe00b2856dc17e580a6a.tar.bz2
Improvements to examples by DKF
Diffstat (limited to 'doc')
-rw-r--r--doc/switch.n75
1 files changed, 49 insertions, 26 deletions
diff --git a/doc/switch.n b/doc/switch.n
index 2ef1cd2..3719a78 100644
--- a/doc/switch.n
+++ b/doc/switch.n
@@ -5,7 +5,7 @@
'\" See the file "license.terms" for information on usage and redistribution
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
'\"
-'\" RCS: @(#) $Id: switch.n,v 1.6 2003/12/14 18:32:36 dkf Exp $
+'\" RCS: @(#) $Id: switch.n,v 1.7 2004/04/22 22:36:22 dkf Exp $
'\"
.so man.macros
.TH switch n 7.0 Tcl "Tcl Built-In Commands"
@@ -109,37 +109,60 @@ several patterns.
Beware of how you place comments in \fBswitch\fR commands. Comments
should only be placed \fBinside\fR the execution body of one of the
patterns, and not intermingled with the patterns.
-.PP
-Below are some examples of \fBswitch\fR commands:
+
+.SH "EXAMPLES"
+The \fBswitch\fR command can match against variables and not just
+literals, as shown here (the result is \fI2\fR):
.CS
-\fBswitch\0abc\0a\0\-\0b\0{format 1}\0abc\0{format 2}\0default\0{format 3}\fR
+set foo "abc"
+switch abc a \- b {expr 1} $foo {expr 2} default {expr 3}
.CE
-will return \fB2\fR,
+
+Using glob matching and the fall-through body is an alternative to
+writing regular expressions with alternations, as can be seen here
+(this returns \fI1\fR):
.CS
-\fBswitch\0\-regexp\0aaab {
- ^a.*b$\0\-
- b\0{format 1}
- a*\0{format 2}
- default\0{format 3}
-}\fR
+switch \-glob aaab {
+ a*b \-
+ b {expr 1}
+ a* {expr 2}
+ default {expr 3}
+}
.CE
-will return \fB1\fR, and
+
+Whenever nothing matches, the \fBdefault\fR clause (which must be
+last) is taken. This example has a result of \fI3\fR:
.CS
-\fBswitch\0xyz {
- a
- \-
- b
- {
- # Correct Comment Placement
- format 1
- }
- a*
- {format 2}
- default
- {format 3}
-}\fR
+switch xyz {
+ a \-
+ b {
+ # Correct Comment Placement
+ expr 1
+ }
+ c {
+ expr 2
+ }
+ default {
+ expr 3
+ }
+}
.CE
-will return \fB3\fR.
+
+.VS 8.5
+When matching against regular expressions, information about what
+exactly matched is easily obtained using the \fB\-matchvar\fR option:
+.CS
+switch -regexp -matchvar foo -- $bar {
+ a(b*)c {
+ puts "Found [string length [lindex $foo 1]] 'b's"
+ }
+ d(e*)f(g*)h {
+ puts "Found [string length [lindex $foo 1]] 'e's and\\
+ [string length [lindex $foo 2]] 'g's"
+ }
+}
+.CE
+.VE 8.5
.SH "SEE ALSO"
for(n), if(n), regexp(n)