Assigning values

In this section we will be creating a nested dictionary containing multiple entries for each record. By using the Tcl dict with command we will then assign variables in the body of our script that are based on the keys stored within our dictionary. The dict with command allows us to accomplish this by assigning entries within our dictionary to variables and then executing a script. The syntax is as follows:

	dict with name keys… script

How to do it…

In the following example, we will create a nested dictionary to hold a collection of key/value pairs and then using the dict for and with commands we will execute a script to output the values to the screen. Return values from the commands are provided for clarity. Enter the following command:


% set person1 [dict create firstname John lastname Smith title Manager]
firstname John lastname Smith title Manager

% set person2 [dict create firstname Mary lastname Jones title Developer]
firstname Mary lastname Jones title Developer}

% set record [dict create 12345 $person1 12346 $person2]
12345 {firstname John lastname Smith title Manager} 12346 {firstname Mary 
lastname Jones title Developer}

% dict for {id info} $record {
puts "Record #: $id"
dict with info {
puts "Title: $title"
puts "Name: $lastname, $firstname"
}
}
Record #: 12345
Title: Manager
Name: Smith, John
Record #: 12346
Title: Developer
Name: Jones, Mary

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.166.149