TCL List.
=======
-->List are ordered collections of elements
-->Tcl commands are basically referred as list. The first entry of the list is the command and other -->entries are the arguments to the command.
-->Each element can be defined either with “” or {}
-->Lists can be created in several ways:
-->By setting a variable to be a list of values
set list {{item 1} {item 2} {item 3}}
-->USing split command
set list [split "item 1.item 2.item 3" "."]
-->Using the list command.
set list [list "item 1" "item 2" "item 3"]
Finally, we get the same output of the above three:{item 1} {item 2} {item 3}
List Commands: Syntax and Examples:
####################
lindex
=======
Syntax: lindex list index
Returns the indexth item from the list.The first item is 0
Example:
--------
set list {one two three}
lindex $list 0
Output:- One
lrange
======
Syntax: lrange list first last
Return one or more adjacent elements from a list Selecting the first two elements:
Example:
--------
lrange {a b c d e} 0 1
Output:- a b
Selecting the last three elements:
lrange {a b c d e} end-2 end
Output:- c d e
llength
=====
Syntax : llength list
Returns the number of elements in a list
Example:
--------
llength {a b c d e}
Output:- 5
lsearch
=====
Syntax : lsearch list pattern
Seraches list for an entry that matches pattern and returns the index for the first match and -1 if there is no match
Example:
-------
set list [list {Washington 1789} {Adams 1797} {Jefferson 1801} \
{Madison 1809} {Monroe 1817} {Adams 1825} ]
set x [lsearch $list Washington*];
set y [lsearch $list Monroe*];
Output: Value of x is 0
Value of y is 4
Foreach
======
Syntax : foreach var list body
It executes the body code one time for each item in the list.
Example:
-------
set tList {one two three}
foreach a $tList {
puts $a
}
Output:
one
two
three
Linsert
=====
Syntax : lindex list index args
Returns a new list with a new list elements inserted just before the indexth element in the list.
Example:
-------
set a [list a b {c d e} f {g h}]
set b [linsert $a 3 "1 2 3"]
Output:
Value of b is : a b {c d e} {1 2 3} f {g h}
Lappend
=======
Syntax : lappend list arg1 arg2….
Appends the args to the list treating each arg as a list element.
Example:
-------
set a [list a b {c d e} f {g h}]
set b [lappend $a "1 2 3"]
Output:
Value of b is : a b {c d e} f {g h} {1 2 3}
Lsort
=====
Syntax : lsort list
It sorts the list and returns a new list in the from of sorted order.
Examples
---------
set yourlist {test2 test3 test1}
puts [lsort $yourlist]
Output:
test1 test2 test3
Note :This will fail if you have numbers > 10:
set yourlist {test2 test3 test1 test11}
puts [lsort $yourlist]
output:
test1 test11 test2 test3
The lsort command with dictionary option does the work for us.
set lis {test1 test10 test20 test15 test3}
puts [lsort -dictionary $lis]
Output:
test1 test3 test10 test15 test20
Lreplace
========
Syntax : lreplace list first last arg1 arg2 …
Example:
-------
set a [list a b c {1 2 3} d e f {g h} {ij K lm}]
set b [lreplace $b 3 5 "AA" "BB"]
Output:
After lreplacing 3 positions with 2 values at position 3:
a b c AA BB f {g h} {ij K lm}
Tcl Programes Demonstrating List Commands Usage.
########################################
1. TCL program to reverse a list.
Solution1:
==========
set my_list "5 7 66 2 1"
set len [llength $my_list]
for {set i $len} {$i > 0} {incr i -1} {
set m [lindex $my_list [expr $i - 1]]
puts -nonewline "$m "
}
Output:1 2 66 7 5
Solution2:
==========
set lst {1 2 3 30 -1 a b}
puts [lreverse $lst]
2. TCL Program to remove duplicate entries from a given list.
Solution1:
=========
set my_list "1 2 3 4 5 6 7 8 11 17 33 3 4 8"
set new_list [lsort -unique $my_list]
puts $new_list
Output:
======
1 11 17 2 3 33 4 5 6 7 8
3.Tcl proogram to increment each elements in a list
set my_list "1 2 3"
foreach li $my_list {
puts [incr li]
}
Output:
2
3
4
4.TCL program to find the number of elements in the list without using llength
set list "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
set i 0
foreach mem $list {
set b "$mem = $i"
incr i
}
puts $i
Output:
-------
15
5. TCl program to reverse a string
set string "manishbidsar"
set rev [string reverse $string]
puts $rev
6.TCL program to convert a list into string.
set string ""
set list {a b c d e f}
for {set i 0} {$i<[llength $list]} {incr i} {
append string [lindex $list $i]
}
puts $string
Output:
abcdef
7.Difference between lappend and concat in TCL.
set list1 {1 2 3}
puts $list1
set list2 {a b c}
puts $list2
set new [lappend list1 $list2]
puts $new
set lengthlist [llength $new]
puts $lengthlist
Output:
1 2 3
a b c
1 2 3 {a b c}
4
set list1 {1 2 3}
puts $list1
set list2 {a b c}
puts $list2
set b [concat $list1 $list2]
puts $b
set concatlength [llength $b]
puts $concatlength
#output
1 2 3
a b c
1 2 3 a b c
6
Note: length of output of lappend is 4 whereas for output of concat is 6
8. Difference in {} and "" in TCL
{ } used to defer the expansion of variable,commands,and backslash characters until the code executes
set i 0
while {$i < 10} {code to execute}
is not the same as
set i 0
while ”$i < 10” ”code to execute”
which will always evaluate as true.
=======
-->List are ordered collections of elements
-->Tcl commands are basically referred as list. The first entry of the list is the command and other -->entries are the arguments to the command.
-->Each element can be defined either with “” or {}
-->Lists can be created in several ways:
-->By setting a variable to be a list of values
set list {{item 1} {item 2} {item 3}}
-->USing split command
set list [split "item 1.item 2.item 3" "."]
-->Using the list command.
set list [list "item 1" "item 2" "item 3"]
Finally, we get the same output of the above three:{item 1} {item 2} {item 3}
List Commands: Syntax and Examples:
####################
lindex
=======
Syntax: lindex list index
Returns the indexth item from the list.The first item is 0
Example:
--------
set list {one two three}
lindex $list 0
Output:- One
lrange
======
Syntax: lrange list first last
Return one or more adjacent elements from a list Selecting the first two elements:
Example:
--------
lrange {a b c d e} 0 1
Output:- a b
Selecting the last three elements:
lrange {a b c d e} end-2 end
Output:- c d e
llength
=====
Syntax : llength list
Returns the number of elements in a list
Example:
--------
llength {a b c d e}
Output:- 5
lsearch
=====
Syntax : lsearch list pattern
Seraches list for an entry that matches pattern and returns the index for the first match and -1 if there is no match
Example:
-------
set list [list {Washington 1789} {Adams 1797} {Jefferson 1801} \
{Madison 1809} {Monroe 1817} {Adams 1825} ]
set x [lsearch $list Washington*];
set y [lsearch $list Monroe*];
Output: Value of x is 0
Value of y is 4
Foreach
======
Syntax : foreach var list body
It executes the body code one time for each item in the list.
Example:
-------
set tList {one two three}
foreach a $tList {
puts $a
}
Output:
one
two
three
Linsert
=====
Syntax : lindex list index args
Returns a new list with a new list elements inserted just before the indexth element in the list.
Example:
-------
set a [list a b {c d e} f {g h}]
set b [linsert $a 3 "1 2 3"]
Output:
Value of b is : a b {c d e} {1 2 3} f {g h}
Lappend
=======
Syntax : lappend list arg1 arg2….
Appends the args to the list treating each arg as a list element.
Example:
-------
set a [list a b {c d e} f {g h}]
set b [lappend $a "1 2 3"]
Output:
Value of b is : a b {c d e} f {g h} {1 2 3}
Lsort
=====
Syntax : lsort list
It sorts the list and returns a new list in the from of sorted order.
Examples
---------
set yourlist {test2 test3 test1}
puts [lsort $yourlist]
Output:
test1 test2 test3
Note :This will fail if you have numbers > 10:
set yourlist {test2 test3 test1 test11}
puts [lsort $yourlist]
output:
test1 test11 test2 test3
The lsort command with dictionary option does the work for us.
set lis {test1 test10 test20 test15 test3}
puts [lsort -dictionary $lis]
Output:
test1 test3 test10 test15 test20
Lreplace
========
Syntax : lreplace list first last arg1 arg2 …
Example:
-------
set a [list a b c {1 2 3} d e f {g h} {ij K lm}]
set b [lreplace $b 3 5 "AA" "BB"]
Output:
After lreplacing 3 positions with 2 values at position 3:
a b c AA BB f {g h} {ij K lm}
Tcl Programes Demonstrating List Commands Usage.
########################################
1. TCL program to reverse a list.
Solution1:
==========
set my_list "5 7 66 2 1"
set len [llength $my_list]
for {set i $len} {$i > 0} {incr i -1} {
set m [lindex $my_list [expr $i - 1]]
puts -nonewline "$m "
}
Output:1 2 66 7 5
Solution2:
==========
set lst {1 2 3 30 -1 a b}
puts [lreverse $lst]
2. TCL Program to remove duplicate entries from a given list.
Solution1:
=========
set my_list "1 2 3 4 5 6 7 8 11 17 33 3 4 8"
set new_list [lsort -unique $my_list]
puts $new_list
Output:
======
1 11 17 2 3 33 4 5 6 7 8
3.Tcl proogram to increment each elements in a list
set my_list "1 2 3"
foreach li $my_list {
puts [incr li]
}
Output:
2
3
4
4.TCL program to find the number of elements in the list without using llength
set list "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
set i 0
foreach mem $list {
set b "$mem = $i"
incr i
}
puts $i
Output:
-------
15
5. TCl program to reverse a string
set string "manishbidsar"
set rev [string reverse $string]
puts $rev
6.TCL program to convert a list into string.
set string ""
set list {a b c d e f}
for {set i 0} {$i<[llength $list]} {incr i} {
append string [lindex $list $i]
}
puts $string
Output:
abcdef
7.Difference between lappend and concat in TCL.
set list1 {1 2 3}
puts $list1
set list2 {a b c}
puts $list2
set new [lappend list1 $list2]
puts $new
set lengthlist [llength $new]
puts $lengthlist
Output:
1 2 3
a b c
1 2 3 {a b c}
4
set list1 {1 2 3}
puts $list1
set list2 {a b c}
puts $list2
set b [concat $list1 $list2]
puts $b
set concatlength [llength $b]
puts $concatlength
#output
1 2 3
a b c
1 2 3 a b c
6
Note: length of output of lappend is 4 whereas for output of concat is 6
8. Difference in {} and "" in TCL
{ } used to defer the expansion of variable,commands,and backslash characters until the code executes
set i 0
while {$i < 10} {code to execute}
is not the same as
set i 0
while ”$i < 10” ”code to execute”
which will always evaluate as true.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.