Tcl switch statement | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Tcl switch statement

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-15-2009
SFNYC SFNYC is offline
Registered User
 
Join Date: Jun 2008
Last Activity: 12 February 2014, 10:30 AM EST
Location: New York City
Posts: 115
Thanks: 0
Thanked 2 Times in 2 Posts
Tcl switch statement

I am just learning Tcl and there are few things about it that is perplexing me. I have a question about the switch statement.

Why are these two switch statements giving me different results?


Code:
$ cat test_switch.tcl
#!/usr/bin/tcl
set foo "abc"

switch abc a - b {puts "No. 1"} $foo {puts "No.2"} default {puts "Default"}

switch abc  {
     a - b    {puts "No.1"}
     $foo     {puts "No.2"}
   default    {puts "Default"}
}

exit 0

$ ./test_switch.tcl
No.2
Default

Sponsored Links
    #2  
Old 05-17-2009
chihung chihung is offline
Registered User
 
Join Date: Jun 2008
Last Activity: 31 July 2012, 10:08 PM EDT
Location: Singapore
Posts: 191
Thanks: 3
Thanked 46 Times in 45 Posts
The "$foo" inside the switch was not replaced and therefore it fall through to the default branch. However, if you set foo to be $foo, it works.


Code:
switch {$foo} {
a - b { puts 1 }
$foo { puts 2 }
default { puts 3 } 
}

I think you got the thing wrong

Code:
% switch
wrong # args: should be "switch ?switches? string pattern body ... ?default body
?"

You should treat the 2nd argument as the input string and match the pattern. In the below example, I use glob to do matching. You can also use regular expression (-regexp) if you want


Code:
switch -glob -- $input {
{to be*} { puts 1 }
{*Not to be} { puts 2 }
{*or*} { puts 3 }
default { puts 4 }
}

Sponsored Links
    #3  
Old 05-17-2009
chihung chihung is offline
Registered User
 
Join Date: Jun 2008
Last Activity: 31 July 2012, 10:08 PM EDT
Location: Singapore
Posts: 191
Thanks: 3
Thanked 46 Times in 45 Posts
BTW, you need to understanding the difference between double quotes ("") and curly braces in Tcl
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Comapilation error with Switch statement jagan_kalluri Programming 1 11-20-2008 04:38 AM
How is use sselect statement o/p in insert statement. nkosaraju Shell Programming and Scripting 2 08-06-2008 09:26 PM
If statement - How to write a null statement april Shell Programming and Scripting 3 04-16-2008 01:14 PM
Switch abey Programming 12 06-27-2006 07:11 PM
using getopt with a switch statement gammarays Shell Programming and Scripting 6 02-09-2005 01:03 AM



All times are GMT -4. The time now is 07:18 AM.