tcl lsearch列表清单 [英] tcl lsearch on list of list

查看:125
本文介绍了tcl lsearch列表清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tcl中有一个列表列表.

set somelist {{aaa 1} {bbb 2} {ccc 1}}

如何搜索列表项的第一项是"bbb"?

我尝试过这种方法,但是没有用.

lsearch $somelist "{bbb *}"

谢谢.

解决方案

使用 -index ,它正是针对这种情况而设计的.正如ramanman指出的那样,当您有列表时,请使用列表过程.您是否考虑过多次比赛会发生什么情况?

就您而言,我会这样做:

% lsearch -index 0 -all -inline $somelist bbb
{bbb 2}
% lsearch -index 0 -all $somelist "bbb"
1
% lsearch -index 0 -all $somelist "ccc"
2

您使用0的 -index 来指定您对外部列表的第一个索引感兴趣. -全部返回所有结果.而且,如果您只想要匹配的list元素的值,则可以使用 -inline ;如果您只想要匹配的元素的索引,则可以忽略它.

There is a list of list in Tcl.

set somelist {{aaa 1} {bbb 2} {ccc 1}}

How to search the list's element which first item is "bbb"?

I tried this way but it doesn't work.

lsearch $somelist "{bbb *}"

Thanks.

解决方案

Use -index, it's designed for exactly this case. As ramanman points out, when you have a list, use list procedures. Have you thought about what happens if you have multiple matches?

In your case, I would just do this:

% lsearch -index 0 -all -inline $somelist bbb
{bbb 2}
% lsearch -index 0 -all $somelist "bbb"
1
% lsearch -index 0 -all $somelist "ccc"
2

You use -index of 0 to specify that you are interested in the first index of the outer list. -all returns all results. And you can use -inline if you just want the value of list element that matches, omit it if you just want the index of the matching element.

这篇关于tcl lsearch列表清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆