Tcl Script Learning 13: List command set

Source: Internet
Author: User

Lesson 13: List Command set

List Arg1 arg2 ... Create a list
Lindex List Index Returns the value of index elements (element) in lists
Llength Lists list elements count


Example①: Create a list, use the list

1. Set L1 [list Sun Mon Tues]
=>sun Mon Tues; #列表 L1 contains three elements

2. Set L2 [list $l 1 Wed]
= = {Sun Mon tues} Wed; #列表 l2 contains two elements. The first element is enclosed in curly braces.

3. Set L3 [list {a $b C} d]
=>{a $b C} D; #花括号阻止引用替换

4. Set L3 [list "a $b C" D]
+ = {A ten C} D


Example②: Llength command

The Llength command can get the number of elements in a list.

Set L1 "1 2 3 4 5"
=>1 2 3 4 5; #定义了一个字符串
Set num [llength $l 1]; #这里 L1 is considered a list.
=>5


Example③: Lindex's study

The lindex command returns a specific element of the specified position in the list. List index count starting from 0!

%set x {1 4 5}
= 1 4 5
% lindex $x 1
=>4
%lindex $x End
=>5
%lindex $x end-1
=>4

Example④: The study of Split

The split command acts in contrast to join, which receives a string and splits it into a given character.
A list. The character used for the split should exist in the string, otherwise split will not search for the corresponding character and the entire
The string is returned as a unique list element, which returns the original string.


Set y [Split 7/4/1776 "/"]
Puts "We celebrate on the [lindex $y 1] ' th day of the [lindex $y 0] ' th month\n '
Split is the split character that separates a string, followed by "/".


Example⑤: Learning from foreach

foreach is the traversal function, and the foreach command/control structure iterates through the list, dropping the value of each element of the list into the specified variable.

1. Single Traversal
Set L1 "I am Zhou Li"
foreach Elem $l 1 {
Puts "---$elem---"
}
=---I---
---am---
---Zhou---
---li---

2. Multiple traversal

foreach {x1 x2} {Orange Blue Red Green Black} x3 {right left up} {
puts [format "x1=%8s x2=%8s x3=%8s" $x 1 $x 2 $x 3]
}
= x1= Orange x2= Blue x3= Right
x1= Red x2= Green x3= left
x1= Black x2= x3= up
x1= x2= x3= Down

Example⑥: Learning from format

Character description
D signed Integer
U unsigned integer
I signed integer. The variable can be in hexadecimal (0x) or octal (0) format
o unsigned octal number
x or x unsigned hexadecimal number
C Maps an integer to the corresponding ASCII character
s string
F Floating point number
Floating-point number represented by E or e scientific notation
The floating-point number represented by G or G in%f or%e format (shorter)
Table 4-3 Format Markers
Flag description
-Align fields to the left
+ Field Right Align
Space is preceded by a number with a space, unless the number has a leading sign. This ranks many numbers in the
Very useful when you are together
0 using 0 as Filler
# leading 0 represents octal, leading 0x indicates hexadecimal number. Always take decimals in floating-point numbers
Point. Do not delete the end of 0 (%g)

1.
#要取第 2 meta values, or 5. The location specifier is in 2$ format and uses \ to refer to the symbol $:
%set res [format "%2\ $s" 1 5 9]
=>5
%puts $res
=>5
%set STR [format]%3\ $s%1\ $s%2\ $s "is" "Right" "You"]
= = Right

2.
%format "%x" 20
=>14; # convert 20 to hexadecimal number
%format "%8x" 20
= = 14; # converts 20 to hexadecimal number, 8-bit data width, right-aligned
%format "%08x" 20
=>00000014; #与上一命令相似, but with 0 add
%format "%-8x" 20
=>14; #宽度 8-bit, left-justified
%format "% #08x" 20
=>0x000014

Set X "a B C"
Puts "Item 2 of the list {$x} is: [Lindex $x 2]\n"
Output the second element of the list listing, see Lindex usage
Set y [Split 7/4/1776 "/"]
Puts "We celebrate on the [lindex $y 1] ' th day of the [lindex $y 0] ' th month\n '
Split is the split character that separates a string, followed by "/".
Set Z [list puts "Arg 2 is $y"]
Puts "A command resembles: $z \ n"

Set I 0;
foreach J $x {
Puts "$J is Item number $i in List X"
INCR i;
}


The study of Fconfigure

The Fconfigure command is used to set or query the properties of an I/O channel. The default settings for channels are for most cases
Applicable. If you are performing event-driven I/O, you may want to set it to non-blocking mode

The study of Fileevent

The #fileevent command registers a command for the I/O channel that is executed when the channel becomes readable or writable
If there is no script parameter in the fileevent command, the command returns the currently registered command and returns an empty string if no registration command is present

Tcl Script Learning 13: List command set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.