Erlang - io:format 's result / (formatting with io_lib:format/2)
io_lib:format
is not an output function the way io:format
is. Instead io_lib:format
only returns the value, but does not output it.
The result of io:format
that you see as "test." is the rendered version as sent to the terminal (including the newline) , then it returns ok
. Conversely, the return value of io_lib:format
that you see as "\"test\".\n"
is simply the erlang shell's representation of the same string, with the quotes and newline escaped, and surrounded by its own quotes.
io_lib:format
is more commonly used for inserting values into the strings (similar to C's printf
functions). For example, doing something like
NewString = io_lib:format("The string entered was ~s I hope you like it",[A])
The value of NewString
would be
The string entered was "test".
I hope you like it
For which the Erlang Shell's representation would be:
"The string entered was \"test\".\n I hope you like it"
If all you want to do is output the value you just entered, then io:format
is sufficient for your needs.
Related videos on Youtube
otisonoza
Updated on September 15, 2022Comments
-
otisonoza about 1 year
I'm trying to get the result of the output of io:format/1.
I know that there's a similar function in io_lib, io_lib:format/2, but the output is different. In fact, it doesn't do anything at all. If I try to bound io:format, ok is bounded, and the formatted string is written out to the console.
So my question is, how can I get the same output with io_lib:format/2? Or how can I bound the formatted string to a variable?
1> A = io:get_line('> '). > "test". "\"test\".\n" 2> io:format(A). "test". ok 3> B = io_lib:format(A, []). "\"test\".\n" 4> B. "\"test\".\n" 5> C = io:format(A). "test". ok 6> C. ok