当前位置:文档之家› Tcl_1_2015.00_LG_03 S公司2015年tcl教程Lab guide

Tcl_1_2015.00_LG_03 S公司2015年tcl教程Lab guide

Tcl_1_2015.00_LG_03 S公司2015年tcl教程Lab guide
Tcl_1_2015.00_LG_03 S公司2015年tcl教程Lab guide

Play with Strings

3

and Lists

Play with Strings and Lists Lab 3-1

Lab 3

Lab 3-2 Play with Strings and Lists

The Power of Tcl 1: Becoming a Proficient Tcl User

Lab Overview

During this lab, you will build a broad and useful command repertoire for

manipulating strings and lists.

Answers & Solutions

This lab guide contains answers and solutions to all questions. If you need help answering a question or would like to confirm your results, check the back portion of this document. Lab Tasks

Create a useful command while applying

the commands learned in lecture

LAB 2Learn skills that will be essential every

time you write Tcl scripts

LAB 3Master the commands to count patterns in

strings, and gain comfort with the basic

components of a Tcl script

LAB 1

Lab 3 File Locations

Directory Structure

design_data ORCA netlist

scripts Constraint and run scripts

libs Technology libraries

tcl_procs Tcl procedures and solutions

.synopsys_pt.setup PrimeTime setup file

.synopsys_dc.setup Design Compiler setup file

Relevant Files

scripts/

rpt_TNV.tcl Incomplete TNV command

run.tcl Reads the ORCA design

tcl_procs/

rpt_TNV.tcl Full TNV solution for homework

list_sub.tcl Solution for Q25 in Task 5

pin_match.tcl Solution for Q26 in Task 5

Play with Strings and Lists Lab 3-3 The Power of Tcl 1: Becoming a Proficient Tcl User

Lab 3

Lab Instructions

Task 1.Playing With Tcl Strings

Before applying strings to real world examples, you will go through some basic

examples to gain an understanding of what strings are all about.

The skills learned will be useful in the future whenever you explore new Tcl

commands or forget how to use commands you learned in the past.

1.Invoke either Design Compiler or PrimeTime.

There is no need to read a design for the following exercises.

All subsequent instructions are generic for either tool - Design

Compiler or PrimeTime.

1.Create two strings by executing the following commands.

https://www.doczj.com/doc/9d13710623.html,e the command regexp –all to count patterns in a string.

Question 1. How many letter l’s are in the string captured by variable a

(you should get 3)?

....................................................................................................

Question 2. How many newlines in the string captured by variable b (you

should get 2)?

.................................................................................................... Lab 3-4 Play with Strings and Lists

The Power of Tcl 1: Becoming a Proficient Tcl User

Lab 3

3.Count the total number of characters in a string using the command string

length. Execute the following example.

Question 3. Did the above exercise confirm that spaces and newlines

count as individual characters in strings?

....................................................................................................

4.Determine if a string matches a pattern using globbing and the command

string match.

For string matching to succeed (i.e. the return value is 1), the pattern and the

string must be identical except that the pattern may include wildcard

characters such as *.

Question 4. Did the above commands work as expected?

....................................................................................................

5.Extract characters from a string using the command string index.

Question 5. Does the index range start at 1 or 0 (i.e. to get the first

character in a string, do you use an index of 1 or 0)?

....................................................................................................

Question 6. Do you need to know the total number of characters in a

string to extract the LAST character of the string?

.................................................................................................... Play with Strings and Lists Lab 3-5 The Power of Tcl 1: Becoming a Proficient Tcl User

Lab 3

Question 7. From your own experimentation, what will string index

return if you provide an index outside the range of the

provided string argument?

....................................................................................................

https://www.doczj.com/doc/9d13710623.html,e the string command to find the index of the last \nt (a newline followed

by the letter t) in the string captured by the variable b.

Use the relevant portion of the man page for the string command shown

below.

string last string1 string2

Search string2 for a sequence of characters that exactly match the

characters in string1. If found, return the index of the first

character in the last such match within string2. If there is no

match, then return -1.

Question 8. What is the index of the first character of the last \nt in the

string captured by variable b?

....................................................................................................

7.Append strings using the append command.

Question 9. After executing the above command, was the value of

variable a modified?

....................................................................................................

Question 10. Does the append command add spaces when it concatenates

strings?

....................................................................................................

8.Visually identify the string commands used so far on job aid #1.

Lab 3-6 Play with Strings and Lists

The Power of Tcl 1: Becoming a Proficient Tcl User

Lab 3 Task 2.Playing With Tcl Lists

Perform the same type of exercises with Tcl lists.

1.Create two Tcl lists by executing the following lines.

2.Count the total number of elements in each list using the Tcl command

llength.

Question 11. Explain why the last example above returned a value of 1?

....................................................................................................

Question 12. Do spaces count as individual list elements?

....................................................................................................

3.Play with list indices. Extract list elements using the command lindex.

Question 13. Write the command to extract the last element from a list.

Use your experience with string indices, or use the man page

for lindex.

.................................................................................................... Play with Strings and Lists Lab 3-7 The Power of Tcl 1: Becoming a Proficient Tcl User

Lab 3

Lab 3-8 Play with Strings and Lists

The Power of Tcl 1: Becoming a Proficient Tcl User

4.

Determine if any list element matches a pattern using lsearch –glob .

Question 14. What does lsearch –glob return if the match is successful and what does it return if the match is NOT successful? .................................................................................................... 5.

Insert items into a list using the command linsert . Question 15. Did the above command modify the variable b ? .................................................................................................... Question 16. Rewrite the above command such that the value of variable b is modified to be "zero one two three". .................................................................................................... 6.

Append lists together using the command lappend .

Question 17. After executing the first command above, was the value of variable b modified? .................................................................................................... Question 18. Does lappend add spaces when appending elements to a list? .................................................................................................... Question 19. Can individual list elements themselves be lists? ....................................................................................................

Lab 3

7.Iterate over every element of a list using the command foreach.

Question 20. How many times does the foreach command iterate?

....................................................................................................

Question 21. What is the user defined variable list_element set to during

each iteration?

....................................................................................................

8.Visually identify the list commands used so far on job aid #1.

Task 3.Convert Strings Into Lists and Back Again

For the last exercise, perform the same type of “hello world” exercises converti ng

strings to lists and back to strings.

1.Create two strings by executing the following command.

2.Convert a string to a list using the split command.

Question 22. Do the splitChars remain in the resulting list?

....................................................................................................

Question 23. Do spaces from the original string remain in the resulting list?

.................................................................................................... Play with Strings and Lists Lab 3-9 The Power of Tcl 1: Becoming a Proficient Tcl User

Lab 3

3.Convert a list back to a string using the join command.

Question 24. Describe the result of the last command executed above.

....................................................................................................

4.Quit the Synopsys tool and return to lecture. This completes the workshop

labs.

Task 4.Optional Homework: Complete rpt_TNV.tcl

Please look at the foils in the appendix of Unit 4 for a description of the problem

you will be solving in this task.

1.Invoke either Design Compiler or PrimeTime using the script ./scripts/run.tcl

2.Play with your starting point by sourcing the procedure contained in

./scripts/rpt_TNV.tcl and executing the command rpt_TNV.

3.Edit the procedure in ./scripts/rpt_TNV.tcl such that the total number of

violations is reported for each individual group (each clock group as well as

each group of design rule violations).

One example solution is contained in ./tcl_procs/rpt_TNV.tcl Task 5. Writing Custom Scripts

Question 25. Write a Tcl script to subtract list_2 elements from list_1 elements

(i.e. list1 – list 2) (Create list1 to be “dc pt mw icc1” and list2 to be “pt icc2 mw”)

……………………………………………………………………

Question 26. Write a Tcl script to check if a pin exists in the input list, and if so

report the net to which it is connected. (The input list is given as

“I_ORCA_TOP/pad_in[10] I_ORCA_TOP/pad_in[5]

I_ORCA_TOP/sd_DQ_in[11]”)

……………………………………………………………………..

Lab 3-10 Play with Strings and Lists

The Power of Tcl 1: Becoming a Proficient Tcl User

Answers / Solutions Lab 3 Play with Strings and Lists

Lab 3-11 The Power of Tcl 1: Becoming a Proficient Tcl User Answers / Solutions

Task 1. Playing with Tcl Strings

Question 1.

How many letter l ’s are in the string captured by the

variable a ?

Question 2. How many newlines are in the string captured by the

variable b?

Question 3. Did the above exercise confirm that spaces and newlines

count as individual characters in strings?

Yes. The command string length will count every

character in a string, which includes newlines and spaces

representing a single character. The double quotes that were

used to create the string in the first place are not part of the

actual string and are not counted in the string length.

Question 4. Did the above commands work as expected?

The first command succeeds because the pattern completely

matches the string.

The second command fails. The pattern and the string must

be identical. The pattern

two* does not completely match

the string! The following example will return a successful

match (notice the wildcard * matches newlines as well).

Question 5. Does the index range start at 1 or 0 (i.e. to get the first

character in a string, do you use an index or 1 or 0)?

0. You will notice later that this is consistent with list

indexing as well.

Lab 3 Answers / Solutions

Lab 3-12 Play with Strings and Lists

The Power of Tcl 1: Becoming a Proficient Tcl User

Question 6. Do you need to know the total number of characters in a

string to extract the LAST character of the string?

No. You can use the string index end to extract the last

character.

Question 7. From your own experimentation, what will string index

return if you provide an index outside the range of the

provided string argument?

The command returns the empty string.

Question 8. What is the index of the first character of the last

\nt in the

string captured by variable b ?

Question 9. After executing the above command, was the value of

variable a modified?

Yes. Certain commands ask for the variable name and will

modify the variable value. Other commands will modify the

string itself (not the variable). To confirm, use printvar as

shown below. Also shown is the relevant portion of the

man page where it clearly asks for the variable itself.

printvar a

→ a = "hello worldone

two

three"

man append

NAME

append - Append to variable SYNOPSIS

append varName ?value value value ...?

DESCRIPTION

Append all of the value arguments to the current value of variable varName.

Answers / Solutions Lab 3 Question 10. Does the append command add spaces when it concatenates

strings?

No, it does not.

Task 2.Playing with Tcl Lists

Question 11. Explain why the last example above returned a value of 1.

The last command returns the length of the list “b” which

has one list element (named b). Notice this is different from

the second to last command which is using the value of the

variable named b (which is set to "one two three" and has

three list elements).

Question 12. Do spaces count as individual list elements?

Spaces are used to separate list elements and are not counted

as list elements themselves. If you wanted to include a

space as a list element, you will have to do something like

the following example.

Question 13. Write the command to extract the last element from a list.

Use your experience with string indices, or use the man

page for lindex.

String and list indices are consistent. Following is the

correct command and the relevant portion of the man page. lindex $a end

man lindex

. . . If index has the value end, it refers to the last element in the list, and end-integer refers to the last element in the list minus the specified integer offset.

Play with Strings and Lists Lab 3-13 The Power of Tcl 1: Becoming a Proficient Tcl User

Lab 3 Answers / Solutions Question 14. What does lsearch –glob return if the match is successful

and what does it return if the match is NOT successful?

This command returns the list index of the 1st matching list

element if successful (recall the first list index starts at 0!).

It returns –1 if not successful. This matches the relevant

portion of the man page shown below.

DESCRIPTION

This command searches the elements of list to see if one of them

matches pattern. If so, the command returns the index of the first matching element. If not, the command returns -1.

Question 15. Did the above command modify the variable b?

No, the variable b is still “one two three”.

Question 16. Rewrite the above command such that the value of variable

b is modified to be "zero one two three".

Question 17. After executing the first command above, was the value of

variable b modified?

Yes, in this example the variable b WAS modified. Use

printvar to validate this.

Question 18. Does lappend add spaces when appending elements to a

list?

Spaces are used to separate elements in a list. Therefore,

lappend MUST add spaces when appending list elements. Lab 3-14 Play with Strings and Lists

The Power of Tcl 1: Becoming a Proficient Tcl User

Answers / Solutions Lab 3

Question 19. Can individual list elements themselves be lists?

Yes. This is shown in the last example as shown below.

The list captured by the variable b is appended to a as a

single list element.

Question 20. How many times does the foreach command iterate?

In this example, 3 times – once for every list element in a.

Question 21. What is the user defined variable list_element set to during

each iteration?

It is set to each subsequent item in the original list a. The

result of the foreach iteration is shown below.

Play with Strings and Lists Lab 3-15 The Power of Tcl 1: Becoming a Proficient Tcl User

Lab 3 Answers / Solutions Task 3.Convert Strings Into Lists and Back Again Question 22. Do the splitChars remain in the resulting list?

No, they do not. The splitChars are removed as well as

identify where the original string should be split into the

resulting list.

Question 23. Do spaces from the original string remain in the resulting

list?

Yes they do. If an individual list element contains spaces, it

will be shown with { } to identify it as a single list element

as shown in the example below.

# The o is removed from the resulting list

# The space in the original list is part of the second list element

# The resulting list contains 3 list elements

split $a o

→ hell { w} rld

# A different example.

# Notice the space is kept as part of the second list element

split $a e

→ h {llo world}

# Notice now that space is used as a splitting element

# The space is removed and the resulting list is split accordingly

split $a "e "

→ h llo world

Question 24. Describe the result of the last command executed above.

The final result is a string where all letter o’s in the origina l

string are replaced by the number 5.

Task 5. Writing Custom Scripts

Question 25. Write a Tcl script to subtract list_2 elements from list_1 elements

(i.e. list1 – list 2)

proc list_sub { lis1 lis2 } {

set result ""

foreach el $lis1 {

if {[lsearch -exact $lis2 $el] == -1} {

lappend result $el

}

}

return $result

}

Lab 3-16 Play with Strings and Lists

The Power of Tcl 1: Becoming a Proficient Tcl User

Answers / Solutions Lab 3 Question 26. Write a Tcl script to check if a pin exists in the

input list, and if so report the net to which it is connected.

(The input list is given as “I_ORCA_TOP/pad_in[10]

I_ORCA_TOP/pad_in[5] I_ORCA_TOP/sd_DQ_in[11]”)

set my_pins [list I_ORCA_TOP/pad_in[10] I_ORCA_TOP/pad_in[5]

I_ORCA_TOP/sd_DQ_in[11]]

proc match_pins { pin_name } {

global my_pins

if {[lsearch -exact $my_pins $pin_name ] != -1} {

set net [get_object_name [get_attribute [get_pins $pin_name -quiet] net]]

puts "The pin $pin_name is connected to the net $net"

} else {

puts "No pin found"

}

}

Note: In the above script try replacing the “-exact” switch with “-

glob” or “-regexp” and check what the script outputs.

Play with Strings and Lists Lab 3-17 The Power of Tcl 1: Becoming a Proficient Tcl User

相关主题
文本预览
相关文档 最新文档