Erlang list item searching

2009-04-12
使用lists:keysearch函数可以方便的对一个[{key, value}, ....]列表里的指定key进行搜索,函数定义如下:
keysearch(Key, N, TupleList) -> {value, Tuple} | false
Key = term()
N = 1..tuple_size(Tuple)
TupleList = [Tuple]
Tuple = tuple()

比如说我们有这样的一个列表:
> Attributes = [{to, "jojo@verse.com"},
{from, "oscar@verse.com/school"},
{type, "chat"},
{'xml:lang', "en"}].

> lists:keysearch(from, 1, Attributes).
{value, {from, "oscar@verse.com/school"}}

若指定Key不存在则返回false
> lists:keysearch(not_exist, 2, Attributes).
false

M-OSCAR | Powered by Blogger | Entries (RSS) | Comments (RSS) | Designed by MB Web Design | XML Coded By Cahayabiru.com