erlang - 读取元组键和值

标签 erlang tuples data-retrieval

如何在 Erlang 中读取元组键和值?

我有这个变量:

Params = [<<"TPUIBrowser">>,0,18,
 {[{<<"End">>,<<"location-1ÿ">>},{<<"Start">>,<<"location-1">>}]},
 null]

我想获取 <<"End">> 的值和<<"Start">> .

我怎样才能在 Erlang 中做到这一点?

我可以这样做:

[_,_,_,A,_] = Params.
{[{_,B},{_,C}]} = A.

但这感觉非常冗长并且容易出错(即当我收到更多参数时)。最好的erlang方式是什么?

最佳答案

lists 中有相关函数图书馆。查看lists:keyfind :

[_,_,_,{A},_] = Params,
{Key, Value} = lists:keyfind(<<"End">>, 1, A).

(我假设您知道 Params 中的 A 位置)

关于erlang - 读取元组键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12134338/

相关文章:

python - super 在子类化 tuple() 时给出错误的类型

python - 元组对列表到字典中

python - 使用可变数量的 for 循环创建元组

java - 使用从其他类的方法中检索到的信息

java - 从 json 中检索图像时出现 "The application may be doing too much work on its main thread"错误

Erlang 中的字符串操作

performance - 用定时器测试 Erlang 函数的性能

android - 如何在服务器上购买空间并将其用于在 android 中存储和检索数据?

erlang - 在 Elixir 中,依赖应用程序是否受到监督?

http - 如何解析带有 `gen_tcp` 的 HTTP 请求?