Erlang字符串列表连接
我有以下属性列表,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的输出和你给定的片段。
That is my output with your given snippet.
是的,代码是正确的。如果您使用 io:format,它将在输出中连接所有可能的内容。
Erlang 中有一个称为 iolist 的元类型,如“可用于输入/输出”。它可以是字符(整数)列表、其他 iolist 或二进制。听起来很混乱,但是很方便。
如果您想要漂亮的打印效果,请使用
~p
格式。换句话说:
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: