Reputation: 121
So I have this weird occurrence in which I delete an element, 5, from a list, but when I return that new list, the output is coming out oddly. I've tested it so far, and there are multiple cases in which this occurs, not just in the code I have below, and also cases in which this doesn't occur, but is still very similar. One example in which it prints out fine is when I delete 10 or 12 instead of 5. So I know from doing lists:member() that 10 does exist within the Newlist, and when I do io:format(), the list comes out correctly. But when I actually return Newlist at the end, I am getting "\n\f" as output, and I'm not understanding why completely. I believe it has something to do with the list not being standard syntax, as stated in the documentation for io:format for what the control sequence ~w does:
"Writes data with the standard syntax. This is used to output Erlang terms. Atoms are printed within quotes if they contain embedded non-printable characters. Atom characters > 255 are escaped unless the Unicode translation modifier (t) is used. Floats are printed accurately as the shortest, correctly rounded string."
Any ideas for why this is occuring and solutions anyone might have so that when the list is returned, it will just be a simple list of [10,12]?
unload_shiptest3(Container) ->
Q = [5,10,12],
Newlist = Q -- [Container],
R = lists:member(10,Q),
io:format("~w~n",[Newlist]),
Newlist.
Upvotes: 1
Views: 78
Reputation: 14042
"/n/f" and [10,12] are the same lists, you can verify it easily in the shell:
9> "\n\f" = [10,12].
"\n\f"
does not throw an error.
In your code, using the explicit format ~w, you get the list uninterpreted: [10,12]
. but if you test your function in the shell, it prints it using pretty print format. The result contains only ASCII characters so it prints it as a string "\n\f"
.
When you test it removing the element 10, the resulting list contains 5 which is not interpreted as a printable character, and the printed result is what you were expecting [5,12]
I think there is a way to ask the shell to avoid the usage of pretty print, I wil edit if I retrieve it.
Upvotes: 2