在 Erlang 中使用 fread 从 user/console 中读取多个单词
Reading multiple words from the user/console using fread in Erlang
我想读取多个单词并使用 Erlang 将它们存储在一个变量中。当我用 fread 记录字符串时,它只记录第一个单词。
1> {ok,[Message]} = io:fread("Type your message : ", "~ts").
Type your message : Hello world
{ok,["Hello"]}
2> world
所以 "Hello" 被保存到 Message 而不是 "Hello world"。如何将这两个词保存到变量消息中。
我正在寻找一个笼统的答案,这样我就可以阅读很多单词,而不仅仅是 2 个单词,所以请不要 post 只有 2 个单词的答案。
期望输出
{ok,["Hello world"]}
1> Line = io:get_line("Type your message: ").
Type your message: Hello world. Goodbye.
"Hello world. Goodbye.\n"
2> Line.
"Hello world. Goodbye.\n"
3> DesiredOutput = {ok, [string:strip(Line, right, $\n)]}.
{ok,["Hello world. Goodbye."]}
4> DesiredOutput.
{ok,["Hello world. Goodbye."]}
Line:
The characters in the line terminated by a line feed (or end of file).
If the I/O device supports Unicode, the data can represent codepoints
> 255 (the latin1 range).
我想读取多个单词并使用 Erlang 将它们存储在一个变量中。当我用 fread 记录字符串时,它只记录第一个单词。
1> {ok,[Message]} = io:fread("Type your message : ", "~ts").
Type your message : Hello world
{ok,["Hello"]}
2> world
所以 "Hello" 被保存到 Message 而不是 "Hello world"。如何将这两个词保存到变量消息中。
我正在寻找一个笼统的答案,这样我就可以阅读很多单词,而不仅仅是 2 个单词,所以请不要 post 只有 2 个单词的答案。
期望输出
{ok,["Hello world"]}
1> Line = io:get_line("Type your message: ").
Type your message: Hello world. Goodbye.
"Hello world. Goodbye.\n"
2> Line.
"Hello world. Goodbye.\n"
3> DesiredOutput = {ok, [string:strip(Line, right, $\n)]}.
{ok,["Hello world. Goodbye."]}
4> DesiredOutput.
{ok,["Hello world. Goodbye."]}
Line:
The characters in the line terminated by a line feed (or end of file). If the I/O device supports Unicode, the data can represent codepoints > 255 (the latin1 range).