TCL array: (Under construction)
:
array exists
array get
array names
array set
array size
array unset
array startsearch
array nextelement
array anymore
array donesearch
array statistics
sat and Saturday are same
wed and Wednesday are same
fri and Friday are same
thu and Thursday are same
sun and Sunday are same
tue and Tuesday are same
mon and Monday are same
Array is a collection of variables.Associative -> abstract data type composed of a collection of key value pairs.- Key value pairs are not ordered.
- Each variable can hold any value or an array.
How to create an array in tcl ?
- We can create an array using
set or "array set"
Different operations associated with array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set days(mon) Monday | |
set days(tue) Tuesday | |
set days(wed) Wednesday | |
set days(thu) Thursday | |
set days(fri) Friday | |
set days(sat) Saturday | |
set days(sun) Sunday | |
foreach key [array names days] { | |
puts "$key and $days($key) are same" | |
} |
Output:
Note: Observe that key and value pairs are not ordered.
Example:2
Output:
4 is associated to Thu
Example:2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#delete existing array | |
array unset days | |
array set days { | |
1 Mon | |
2 Tue | |
3 Wed | |
4 Thu | |
5 Fri | |
6 Sat | |
7 Sun | |
} | |
foreach key [array names days] { | |
puts "$key is associated to $days($key) " | |
} |
4 is associated to Thu
5 is associated to Fri
1 is associated to Mon
6 is associated to Sat
2 is associated to Tue
7 is associated to Sun
3 is associated to Wed
All array operations are listed in this example
Example:3
Example:4
All array operations are listed in this example
Example:3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array set days { | |
1 Monday | |
2 Tuesday | |
3 Wednesday | |
4 Thursday | |
5 Friday | |
6 Saturday | |
7 Sunday | |
8 Jollyday | |
} | |
if [array exists days] { puts "Array days exists"} | |
unset days(8) | |
puts "size of array is [array size days]" | |
set start [array startsearch days] | |
while {[array anymore days $start]} { | |
set key [array nextelement days $start] | |
puts $days($key) | |
} | |
array donesearch days $start | |
puts "printing array using foreach loop" | |
foreach key [array names days] { | |
puts "$key is associated with $days($key)" | |
} | |
set i 1 | |
puts "printing array using while loop" | |
while { $i <= [array size days] } { | |
puts "$i is associated with $days($i)" | |
incr i | |
} | |
array statistics days | |
array unset days |
Example:4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc addname {first last} { | |
global name | |
incr name(ID) | |
set id $name(ID) | |
set name($id,first) $first | |
set name($id,last) $last | |
} | |
#Initialization | |
set name(ID) 0 | |
addname Teja Konduri | |
addname Mark Zuckerberg | |
addname Steve Jobs | |
addname Kate Twinslet | |
addname Deepika Padukone | |
addname Katrina Kaif |
No comments:
Post a Comment