4

In some weird way all the numbers over 8, single, in a list becomes some kind of ASCII?

[8] -> ["\b"]

Please try to help me with this one :)

2240
  • 1,547
  • 2
  • 12
  • 30
Johnny
  • 41
  • 1
  • Show us code where this is happening. – arunkumar Sep 10 '11 at 13:23
  • just write it shell, [8]. returns ["\b"], how can I avoid this ? – Johnny Sep 10 '11 at 13:24
  • i want it to return [8] to me ofcourse. – Johnny Sep 10 '11 at 13:26
  • 3
    This should help explain it - http://learnyousomeerlang.com/starting-out-for-real#lists – arunkumar Sep 10 '11 at 13:43
  • See also [this question](http://stackoverflow.com/q/2348087/113848). – legoscia Jan 18 '13 at 14:39
  • 1
    Actually you can change this behaviour. Look at [this question](http://stackoverflow.com/questions/25978873/avoid-converting-numbers-to-characters-in-erlang) – mpm Sep 23 '14 at 12:34
  • Does this answer your question? [Can I disable printing lists of small integers as strings in Erlang shell?](https://stackoverflow.com/questions/2348087/can-i-disable-printing-lists-of-small-integers-as-strings-in-erlang-shell) – 2240 Jan 26 '21 at 12:06

3 Answers3

9

String is not a data type in Erlang, it's just a list of integers. But Erlang shell try to display lists as strings if possible:

1> S = [65, 66, 67, 68, 69, 70].
"ABCDEF"
2> S = "ABCDEF".
"ABCDEF"
3> io:write(S).
[65,66,67,68,69,70]ok
4> [65, 66].
"AB"
5> [65, 66, 1].
[65,66,1]
hdima
  • 3,627
  • 1
  • 19
  • 19
  • But is there any way to solve it ? So it returns the number in list instead of the "string" ?? – Johnny Sep 11 '11 at 09:00
  • Actually, it's not a bug, it's feature. You always can use io:write/1 to print a list as is. You can also define a shorter alias for io:write/1 with shell_default or user_default modules: http://www.erlang.org/doc/man/shell_default.html – hdima Sep 11 '11 at 12:41
2

Print it with ~w instead of ~p, and your issue should go away.

~p tries to interpret the elements in the list as ASCII. ~w does not.

2240
  • 1,547
  • 2
  • 12
  • 30
EvilTeach
  • 28,120
  • 21
  • 85
  • 141
0

From documentation: http://www.erlang.org/doc/reference_manual/data_types.html

2.11 String

Strings are enclosed in double quotes ("), but is not a data type in Erlang. Instead a string "hello" is shorthand for the list [$h,$e,$l,$l,$o], that is [104,101,108,108,111].

Two adjacent string literals are concatenated into one. This is done at compile-time and does not incur any runtime overhead. Example:

"string" "42"

is equivalent to

"string42"

flapjack
  • 704
  • 1
  • 8
  • 13