Tcl -- lsearch

来源:互联网 发布:619什么意思网络用语 编辑:程序博客网 时间:2024/06/05 21:11

1. lsearch - See if a list contains a particular element.

    看一个list中是否包含一个指定的元素。

    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 (unless the options -all or -inline are specified.)

    If not, the command returns -1.

    即:这个命令是用来在一个列表中搜索满足条件的元素。

    在没有设定-all或者-inline选项的情况下,如果有满足条件的元素,则返回满足条件的第一个元素的下标,如果没有就返回-1.

2. 语法

    lsearch ?-exact? ?-glob? ?-regexp? list pattern   

    返回list 中第一个匹配模式 pattern 的元素的索引,如果找不到匹配就返回-1。

    -exact、-glob 、-regexp 是三种模式匹配的技术。-exact表示精确匹配

    -glob 的匹配方式和 string match命令的匹配方式相同,-regexp 表示正规表达式匹配。

    缺省时使用-glob 匹配。

    The option arguments indicates how the elements of the list are to be matched against pattern and must have one of the values below:

    a. 匹配样式选项:

    If all matching style options are omitted, thedefault matching styleis -glob.

    If more than one matching style is specified, the last matching stylegiventakes precedence(优先).

                           - exact

                           - glob

                           - regexp

                           - sorted

    b. 内容描述选项:

    These options describe how to interpret the items in the list being searched.

    They are only meaningful when used with the -exact and -sortedoptions.

    If more than one is specified, the last one takes precedence. The default is -ascii.

                           - ascii

                           - dictionary

                           - integer

                           - nocase

                           - real

   c. 排序列表选项:

                           - decreasing

                           - increasing

   d. 嵌套(内嵌)列表选项:

       这些可选项被用来寻找子列表,可以与任何其他可选项一起使用。

                           - index indexList

      这个可选项寻找内嵌的列表,indexList变元必须给出一个索引列表(与lindex和lset类似)

      indexList给出的索引列表在list中必须存在对应的索引,否则会出错。

                           - subindices

     这个可选项只返回寻找到的子列表元素,必须和-index配合使用。

   e. 通用修饰语选项:

                           - all(返回所有满足条件的下标)

   Changes the result to be the list of all matching indices (or all matchingvalues if -inline is specified as well.)

   If indices are returned, the indices will be in numeric order.

   If values are returned, the order of the values will be the order of those values within the input.

   返回一个列表,列表的元素为所有匹配到的索引,返回的索引按照数字顺序排列。

   如果同时指定了-inline选项,则返回数值,数值的顺序就是在列表中的顺序。

                            - inline  (返回的是值不是下标)

   The matching value is returned instead of its index (or an empty string if no value matches.)

   If -all is also specified, then the result of the command is the list of all values that matched.

   

  可以看出:

  1)指定-all, 未指定inline时,返回的是所有满足条件的下标(索引)

  2)指定了-all并且也指定了-inline时, 返回的是所有满足条件的下标对应的值

  3)-all和-inline都不指定时,返回的是第一个满足条件的下标

                           - not  不满足条件

                           - start index  起始的索引(下标)

   

   1)-not 不满足c*条件的有lump和less,对应的下标分别是0和3.

   2)不满足条件,并且从下标2开始,所有只有下标3满足条件。

   3)验证是对的,lump--下标0,less--下标3.

3.  例子

    



0 0
原创粉丝点击