Saturday, September 7, 2013

Basic Tcl commands-part2


Tcl command Reference : http://www.tcl.tk/man/tcl8.5/tutorial/tcltutorial.html
Online Tcl script editor :http://www.compileonline.com/execute_tcl_online.php

To check the tcl output please copy paste the below code in online tcl script editor and for more detailed explanation use the above reference .

For Tcl tutorial Part-1 click here


Conditions :
 set k  0
 puts "$k"
 % 0  
set result [ expr {($k == 0) ? "true":"false"} ] 
puts "$result"
 % true  

Increment :
set k 0
incr k
puts "k=$k"
 % k=1  
incr k 
puts "k=$k"
 % k=2  
incr k 10 
puts "k=$k"
 % k=12  

String Operations :-


String matching :
Syntax : string match <str1> <str2>
             matches the occurrence of str1 in str2.

set str1    "*=*"
set string1  "RUN_TIME = 1000"
puts  [string match $str1  $string1]
 % 1  

String first, last, compare :

compare :
compare performs a character by character comparison.
set string1    "this is sparta..."
set str1        "sparta"
puts [ string compare $string1 $str1 ]
 % 1  
puts [ string compare $str1 $string1 ]
 % -1  
puts [ string compare $string1 $string1 ]
 % 0  
If 1st string is greater than 2nd ,then return 1.
If 1st string is less than 2nd , then return -1 .
If 1st string is Equal to 2nd , then return 0.

first and last :
It will return the index position for first or last occurrence of a char or string.

set joker    "It's a funny world we live in. Speaking of which, do you know how I got these scars?"

puts [string first  "I"  $joker]
puts [string first  "a"  $joker]
puts [string first  "funny"   $joker]
 % 0  
 % 5  
 % 7  
puts [string last  "I"  $joker
puts [string last  "a"  $joker
puts [string last  "funny"  $joker]
 % 66  
 % 80  
 % 7  

Index :
Index returns a single character from string at index position number.

set xyz  "this is ...!"
puts  [ string index  $xyz  0 ]
 % t  
puts   [ string index $xyz 1 ]
 % h  
puts  [ string index $xyz end ]
 % !  

Range :
set  xyz      "this is ...!"
puts [ string range  $xyz  3  end ]
 % s is ...!   

tolower, toupper :
These arguments return a string made from the characters of string converted to the appropriate case.

tolower :
set  string1    "ToM RiDdLe"
puts [ string  tolower  $string1 ]
 % tom riddle  

toupper :
set  string1    "ToM RiDdLe"
puts [ string  toupper  $string1 ]
 % TOM RIDDLE  

String Length :

puts [ string length  "uzumaki naruto" ]
 % 16  

Split and Join Cmds :
set yyy  "din1 20 100,din2 40 80,din3 8 9,din4 25 40"

puts [ join [split $yyy ","] ]
 % din1 20 100 din2 40 80 din3 8 9 din4 25 40   

puts [ join [split $yyy ","] ":" ]
 % din1 20 100:din2 40 80:din3 8 9:din4 25 40  






No comments:

Post a Comment