পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Tuesday, April 4, 2017

Tcl - Dictionary

A dictionary is an arrangement for mapping values to keys. The syntax for the conventional dictionary is shown below −
dict set dictname key value
# or 
dict create dictname key1 value1 key2 value2 .. keyn valuen
Some examples for creating a dictionary are shown below −
#!/usr/bin/tclsh

dict set colours  colour1 red 
puts $colours
dict set colours  colour2 green
puts $colours

set colours [dict create colour1 "black" colour2 "white"]
puts $colours
When the above code is executed, it produces the following result −
colour1 red
colour1 red colour2 green
colour1 black colour2 white

Size of Dict

The syntax for getting size of dict is shown below −
[dict size dictname]
An example for printing the size is shown below −
#!/usr/bin/tclsh

set colours [dict create colour1 "black" colour2 "white"]
puts [dict size $colours]
When the above code is executed, it produces the following result −
2

Dictionary Iteration

A simple dictionary iteration for printing keys and valued of the dictionary is shown below −
#!/usr/bin/tclsh

set colours [dict create colour1 "black" colour2 "white"]
foreach item [dict keys $colours] {
   set value [dict get $colours $item]
   puts $value
}
When the above code is executed, it produces the following result −
black
white

Value for Key in Dict

The syntax for retrieving value for key in dict is shown below −
[dict get $dictname $keyname]
An example for retrieving value for key is given below −
#!/usr/bin/tclsh

set colours [dict create colour1 "black" colour2 "white"]
set value [dict get $colours colour1]
puts $value
When the above code is executed, it produces the following result −
black

All Keys in Dict

The syntax for retrieving all keys in dict is shown below −
[dict keys $dictname]
An example for printing all keys is shown below −
#!/usr/bin/tclsh

set colours [dict create colour1 "black" colour2 "white"]
set keys [dict keys $colours]
puts $keys
When the above code is executed, it produces the following result −
colour1 colour2

All Values in Dict

The syntax for retrieving all values in dict is shown below −
[dict values $dictname]
An example for printing all values is shown below −
#!/usr/bin/tclsh

set colours [dict create colour1 "black" colour2 "white"]
set values [dict values $colours]
puts $values
When the above code is executed, it produces the following result −
black white

Key Exists in Dict

The syntax for checking if a key exists in dict is shown below −
[dict exists $dictname $key]
An example for checking if a key exists in dict is shown below −
#!/usr/bin/tclsh

set colours [dict create colour1 "black" colour2 "white"]
set result [dict exists $colours colour1]
puts $result
When the above code is executed, it produces the following result −
1

No comments:

Post a Comment