Can I disable printing lists of small integers as strings in Erlang shell?
Asked Answered
B
9

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.

Burk answered 23/9, 2014 at 12:42 Comment(0)
P
23

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.

Pinetum answered 27/2, 2010 at 16:54 Comment(2)
I do know about io:format and should probably have mentioned it in the question :)Assent
This is exactly what I was looking for when I needed to print a list of integers as a string. Thanks!Letishaletitia
C
9

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"

Callahan answered 27/2, 2010 at 20:17 Comment(0)
B
9

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.

Burk answered 23/9, 2014 at 12:42 Comment(0)
L
7

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]}
Lionize answered 23/9, 2014 at 10:18 Comment(0)
A
2

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)]).

Abrade answered 28/2, 2010 at 16:47 Comment(0)
F
1

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)).
Francinefrancis answered 10/3, 2010 at 11:27 Comment(0)
I
0

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.

Illuminance answered 27/2, 2010 at 19:7 Comment(1)
@Zed: I was aware of that. But I guess I've misunderstood the question. Now I got what he's trying to do. Thx.Illuminance

© 2022 - 2024 — McMap. All rights reserved.