首页/文章/ 详情

基于Python语言的二次开发-Tcl语言基础回顾-Lists(2/12)

3月前浏览1306

All variables in Tcl are lists. Lists can contain integers (such as list of node IDs), real numbers (such as coordinates of a node), strings, lists of other lists, or a combination of all the above. A standard variable in Tcl is a list of length 1.

A list can be converted to a string when manipulated by string commands, a list can be part of an array, and an array can be converted to and from a list. Regardless of the contents, the means of manipulating lists does not change.

The following list contains some commonly used list manipulation commands and a summary of their usage. For more in-depth explanations and a complete listing, referred to http://www.tcl.tk/man/ or to a Tcl/Tk handbook.

  • list 

  • Creates a list from 

  • lappend 

  • Appends , , etc… as a single entry in the list listvarname. Note that there is no $ before listvarname, as the original list is modified.

  • lindex 

  • Extracts an entry from a list by referencing its location in the list.

  • linsert 

  • Returns a list consisting of the entry or entries contained in  inserted into the list listvarname at the position given by index.

  • llength 

  • Returns the number of entries in the list, that is, the length of the list.

  • lrange 

  • Extracts a sequential range of entries from a list.

  • lreplace 

  • Replaces the entries at index1 through  with the entries specified by , , etc…

  • lsearch 

  • Returns the index of the first occurrence described by pattern.

  • lsort 

  • Returns a list with entries sorted according to an array of options.

  • concat 

  • Joins together multiple lists.

  • join 

  • Generates a string from a list by combining the list contents using the characters in .

  • split 

  • Creates a list from a string by breaking the string at each occurrence of the characters included in charset.

Indices for lists always begin with 0. Instead of using the index, the last entry can alternatively be referenced by using the special index , the next to last entry by end-1, etc…

Lists are automatically defined when variable data contains spaces.

set item_list "item1 item2 item3 item4";
item1 item2 item3 item4

To find the length of a list:

llength $item_list;
4

To return a value in a list:

lindex $item_list 0;
item1

lindex $item_list 3;
item4

lindex $item_list end;
item4

lindex $item_list end-1;
item3

To return a range of values from a list:

lrange $item_list 1 3;
item2 item3 item4

Sub-lists are easily created by grouping items in lists with braces, {}. An example using HyperGraph is to create a list of curves for each window on a page.

set page_list "{p1w1c1 p1w1c2 p1w1c3} {p1w2c1 p1w2c2 p1w2c3}";
lindex $page_list 1;
p1w2c1 p1w2c2 p1w2c3

set page2 "[lindex $page_list 1]";
lindex $page2 0;
p1w2c1


来源:TodayCAEer
ACT二次开发UGpythonUMOrigin
著作权归作者所有,欢迎分享,未经许可,不得转载
首次发布时间:2024-08-14
最近编辑:3月前
TodayCAEer
本科 签名征集中
获赞 18粉丝 28文章 243课程 0
点赞
收藏
作者推荐

HyperWork 2024 -二次开发-Tcl语言-Boolean (6/12)

Booleanstatementsallowyoutoincorporatelogicstatementsintoyourcontrolstructuresandmathematicalexpressionsforevaluation.ThefollowinglistcontainssomecommonlyusedlistBooleanexpressionsandasummaryofwhattheymean.>Greaterthan<Lessthan!=Notequalto==Equalto<=Lessthanorequalto>=GreaterthanorequaltoFormorein-depthexplanationsandacompletelisting,referredtohttp://www.tcl.tk/man/ortoaTcl/Tkhandbook.来源:TodayCAEer

未登录
还没有评论
课程
培训
服务
行家
VIP会员 学习 福利任务 兑换礼品
下载APP
联系我们
帮助与反馈