我可以在 Erlang shell 中禁用将小整数列表打印为字符串吗?

发布于 2024-08-23 00:55:53 字数 155 浏览 8 评论 0原文

Erlang shell“猜测”给定列表是否是可打印字符串,并以方便的方式打印它. 这个“便利”可以被禁用吗?

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

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

发布评论

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

评论(7

止于盛夏 2024-08-30 00:55:53

我不知道是否可以更改 shell 的默认行为,但您至少可以使用 io:格式

这是一个示例:

1> io:format("~p~n", [[65, 66, 67]]).
"ABC"
ok
2> io:format("~w~n", [[65, 66, 67]]).
[65,66,67]
ok

由于 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:

1> io:format("~p~n", [[65, 66, 67]]).
"ABC"
ok
2> io:format("~w~n", [[65, 66, 67]]).
[65,66,67]
ok

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.

养猫人 2024-08-30 00:55:53

您可以使用 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.

够运 2024-08-30 00:55:53

我倾向于通过在 shell 的列表中添加一个原子来实现这一点。

例如:

Eshell V5.7.4  (abort with ^G)
1> [65,66,67].
"ABC"
2> [a|[65,66,67]].
[a,65,66,67]

当然也可以是[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:

Eshell V5.7.4  (abort with ^G)
1> [65,66,67].
"ABC"
2> [a|[65,66,67]].
[a,65,66,67]

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"

无所谓啦 2024-08-30 00:55:53

从 Erlang/OTP R16B 开始,您可以使用该函数 shell:strings/1 打开或关闭此功能。请注意,它还会影响实际应该是字符串的内容的打印,例如以下示例中的 "foo"

1> {[8,9,10], "foo"}.
{"\b\t\n","foo"}
2> shell:strings(false).
true
3> {[8,9,10], "foo"}.   
{[8,9,10],[102,111,111]}

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:

1> {[8,9,10], "foo"}.
{"\b\t\n","foo"}
2> shell:strings(false).
true
3> {[8,9,10], "foo"}.   
{[8,9,10],[102,111,111]}
箜明 2024-08-30 00:55:53

不,没有办法禁用它。我发现的最好的替代方法是显式打印查询中的值(使用 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)]).

狼性发作 2024-08-30 00:55:53

我认为你无法阻止它。
在前面添加一个原子看起来像是一个拼凑 - 它确实改变了你的原始字符串。

我通常使用 list:flatten(String) 将其强制为字符串 - 特别是 io_lib:format() 的返回值并不总是打印为字符串。在其上使用lists:flatten()使其成为一个。

我使用以下“C 风格”:

sprintf(Format) ->
     sprintf(Format, []).
sprintf(Format, Args) ->
    lists:flatten(io_lib:format(Format, Args)).

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":

sprintf(Format) ->
     sprintf(Format, []).
sprintf(Format, Args) ->
    lists:flatten(io_lib:format(Format, Args)).
友欢 2024-08-30 00:55:53

问题是字符串不是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文