我如何用erlang api解析标准输入? [英] How can i parse the standard input with the erlang api?

查看:77
本文介绍了我如何用erlang api解析标准输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Erlang开发一个游戏,现在我需要阅读标准的输入。我试过以下电话:

  io:fread()
io:read()

问题是,当包含空格时,我无法读取整个字符串。所以我有以下问题:


  1. 当他按Enter键时,我如何读取用户输入的字符串? (请记住,字符串包含空格)

  2. 如何转换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:

  1. How can i read the string typed from the user when he press the enter key? (remember that the string contains white spaces)
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆