我可以在 Erlang shell 中禁用将小整数列表打印为字符串吗?
Erlang shell“猜测”给定列表是否是可打印字符串,并以方便的方式打印它. 这个“便利”可以被禁用吗?
The Erlang shell "guesses" whether a given list is a printable string and prints it that way for convenience. Can this "convenience" be disabled?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我不知道是否可以更改 shell 的默认行为,但您至少可以使用 io:格式。
这是一个示例:
由于 shell 仅用于实验/维护,因此 io:format() 至少应该足以满足您的实际应用程序。也许您还应该考虑编写自己的格式/打印方法,例如
formatPerson()
或类似的方法,它可以很好地格式化所有内容。I don't know if it's possible to change the default behavior of the shell, but you can at least format your output correctly, using io:format.
Here is an example:
And since the shell is only for experimenting / maintenance,
io:format()
should be at least enough for your real application. Maybe you should also consider to write your own format/print method, e.g.formatPerson()
or something like that, which formats everything nicely.您可以使用
shell:strings/1
函数禁用此类行为 从 Erlang R16B 开始。请记住,这是所有节点 shell 的全局选项,在寿命较长的节点中完成 shell 操作后,最好将其重新设置。
You can disable such behavior with
shell:strings/1
function starting with Erlang R16B.Just remember that this is option global to all node shells, and it might be wise to set it back after finishing playing is shell in longer living nodes.
我倾向于通过在 shell 的列表中添加一个原子来实现这一点。
例如:
当然也可以是[a,65,66,67]。但 [a|fun_that_returns_a_list()] 会打印“大多数时候是正确的事情”
I tend to do it by prepending an atom to my list in the shell.
for example:
could also be [a,65,66,67], of course. but [a|fun_that_returns_a_list()] will print "the right thing(ish) most of the time"
从 Erlang/OTP R16B 开始,您可以使用该函数 shell:strings/1 打开或关闭此功能。请注意,它还会影响实际应该是字符串的内容的打印,例如以下示例中的
"foo"
:As of Erlang/OTP R16B, you can use the function shell:strings/1 to turn this on or off. Note that it also affects printing of things that are actually meant to be strings, such as
"foo"
in the following example:不,没有办法禁用它。我发现的最好的替代方法是显式打印查询中的值(使用 io:format )或事后执行: io:format("~w\n", [ v(-1)])。
No, there is no way to disable it. The best alternative I find is to either explicitly print out the value in the query (with
io:format
) or after the fact do:io:format("~w\n", [v(-1)])
.我认为你无法阻止它。
在前面添加一个原子看起来像是一个拼凑 - 它确实改变了你的原始字符串。
我通常使用 list:flatten(String) 将其强制为字符串 - 特别是 io_lib:format() 的返回值并不总是打印为字符串。在其上使用lists:flatten()使其成为一个。
我使用以下“C 风格”:
I don't think you can prevent it.
Prepending an atom seems like a kludge - it does alter your original string.
I typically use lists:flatten(String) to force it to a string - especially the returnvalue of io_lib:format() does not always print as a string. Using lists:flatten() on it makes it one.
I use the following "C-style":
问题是字符串不是 Erlang 中的类型。字符串只是整数列表,因此 shell 无法区分可打印字符串和通用列表。不知道这是否能回答你的问题。
The problem is that the string is not a type in Erlang. A string is just a list of integers, so there's no way for the shell to distinguish a printable string from a generic list. Don't know if this answer to your question.