我如何用erlang api解析标准输入? [英] How can i parse the standard input with the erlang api?
问题描述
io:fread()
io:read()
问题是,当包含空格时,我无法读取整个字符串。所以我有以下问题:
- 当他按Enter键时,我如何读取用户输入的字符串? (请记住,字符串包含空格)
- 如何转换56号字符串中的56字符串?
阅读行
您可以使用 io:get_line / 1
可以通过换行符获取字符串从控制台。
3> io:get_line(Prompt>)。
提示>你好你好吗
你好,你好吗?\\\
io:读取
会得到你的erlang术语,所以你不能读一个字符串,除非你想让你的用户用引号括起字符串。
中的模式io:fread
似乎没有让您读取任意长度的包含空格的字符串。
解析整数
您可以将56
转换为 56
使用 erlang:list_to_integer / 1
。
5>二郎:list_to_integer( 56)。
56
或使用 string:to_integer / 1
,这也将返回你的其他字符串
10>字符串:to_integer( 56hello)。
{56,你好}
11>字符串:to_integer( 56)。
{56,[]}
I'm developing a game in Erlang, and now i need to read the standard input. I tried the following calls:
io:fread()
io:read()
The problem is that i can't read a whole string, when it contains white spaces. So i have the following questions:
- How can i read the string typed from the user when he press the enter key? (remember that the string contains white spaces)
- How can i convert a string like "56" in the number 56?
Read line
You can use io:get_line/1
to get string terminated by line feed from console.
3> io:get_line("Prompt> ").
Prompt> hello world how are you?
"hello world how are you?\n"
io:read
will get you erlang term, so you can't read a string, unless you want to make your users wrap string in quotes.
Patterns in io:fread
does not seem to let you read arbitrary length string containing spaces.
Parse integer
You can convert "56"
to 56
using erlang:list_to_integer/1
.
5> erlang:list_to_integer("56").
56
or using string:to_integer/1
which will also return you the rest of a string
10> string:to_integer("56hello").
{56,"hello"}
11> string:to_integer("56").
{56,[]}
这篇关于我如何用erlang api解析标准输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!