Erlang字符串列表连接

发布于 2025-01-05 05:59:59 字数 532 浏览 1 评论 0原文

我有以下属性列表,

PropList = [{numbers, ["22", "10"]}, {etc, "22"}].

我希望像这样取出列表 ["22", "10"]

proplists:get_value(numbers, PropList).

问题是我将列表中的两个字符串连接起来,即 “2210”。我尝试使用 propertylists:lookup/2 获取整数元组和模式匹配来提取列表。但我最终还是得到了“2210”

我猜这是因为 erlang 在内存中存储字符串的方式。有人可以帮助我吗?

稍后编辑:如果我对字符串列表进行映射,我已经设法提取并使用数据......也许这只是一个打印问题?

稍后编辑 我不知道发生了什么,也许我太累了:) 对不起,大家。明天删除这个问题

I have the following property list

PropList = [{numbers, ["22", "10"]}, {etc, "22"}].

I wish to get out the list ["22", "10"] like this:

proplists:get_value(numbers, PropList).

The problem is that I get the two strings inside the list concatenated, ie "2210". I tried using propertylists:lookup/2 to get the whole numbers tuple and pattern match to extract the list. But I still end up getting "2210".

I'm guessing it's because of the way erlang stores strings in memory.. Can someone help me here?

Later Edit: I've managed to extract and use the data if i do a map over the list of strings... Maybe this is just a printing issue?

Later-Later Edit I don't know what happened, maybe I'm too tired :) sorry guys. Will delete this question tomorrow

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁的新欢旧爱 2025-01-12 05:59:59
1> PropList = [{numbers, ["22", "10"]}, {etc, "22"}].       
[{numbers,["22","10"]},{etc,"22"}]

2> proplists:get_value(numbers, PropList).
["22","10"]

3> 

这是我的输出和你给定的片段。

1> PropList = [{numbers, ["22", "10"]}, {etc, "22"}].       
[{numbers,["22","10"]},{etc,"22"}]

2> proplists:get_value(numbers, PropList).
["22","10"]

3> 

That is my output with your given snippet.

两相知 2025-01-12 05:59:59

是的,代码是正确的。如果您使用 io:format,它将在输出中连接所有可能的内容。

Erlang 中有一个称为 iolist 的元类型,如“可用于输入/输出”。它可以是字符(整数)列表、其他 iolist 或二进制。听起来很混乱,但是很方便。

如果您想要漂亮的打印效果,请使用 ~p 格式。

换句话说:

2> io:format([[65, $B, 67], <<"DEF">>, [[71, 72], 73], "JKL", 10]).
ABCDEFGHIJKL
ok
3> io:format("~p", [[[65, $B, 67], <<"DEF">>, [[71, 72], 73], "JKL", 10]]).
["ABC",<<"DEF">>,["GH",73],"JKL",10]ok

Yes, the code is correct. If you use io:format, it will concatenate all it can on output.

There is a meta-type in Erlang called iolist as in "can be used for input/output". It can be a list of characters (integers), other iolists or a binary. Sounds confusing, but quite handy.

If you want to pretty-print, use the ~p format.

In other words:

2> io:format([[65, $B, 67], <<"DEF">>, [[71, 72], 73], "JKL", 10]).
ABCDEFGHIJKL
ok
3> io:format("~p", [[[65, $B, 67], <<"DEF">>, [[71, 72], 73], "JKL", 10]]).
["ABC",<<"DEF">>,["GH",73],"JKL",10]ok
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文