热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在Erlang中读取整数?-HowtoreadintegerinErlang?

Imtryingtoreaduserinputofinteger.(likecin>>nInput;inC++)Ifoundio:freadbiffr

I'm trying to read user input of integer. (like cin >> nInput; in C++)
I found io:fread bif from http://www.erlang.org/doc/man/io.html, so I write code like this.

我正在尝试读取整数的用户输入。 (比如cin >> nInput;在C ++中)我从http://www.erlang.org/doc/man/io.html找到了io:fread bif,所以我写了这样的代码。

{ok, X} = io:fread("input : ", "~d"),
io:format("~p~n", [X]).

{ok,X} = io:fread(“input:”,“〜d”),io:format(“〜p~n”,[X])。

but when I input 10, the erlang terminal keep giving me "\n" not 10. I assume fread automatically read 10 and conert this into string. How can I read integer value directly? Is there any way to do this? Thank you for reading this.

但是当我输入10时,erlang终端一直给我“\ n”而不是10.我假设fread自动读取10并将其变为字符串。我怎样才能直接读取整数值?有没有办法做到这一点?谢谢您阅读此篇。

5 个解决方案

#1


1> {ok, [X]} = io:fread("input : ", "~d").
input : 10
{ok,"\n"}
2> X.
10
3> {ok, [A,B]} = io:fread("input : ", "~d,~d").
input : 456,26
{ok,[456,26]}

That's all.

#2


There are various functions in OTP to help you convert a string to an integer. If you just read a string from the user (until newline for example) you can the evaluate it with the function to_integer(String) in the string module:

OTP中有各种函数可以帮助您将字符串转换为整数。如果您只是从用户读取一个字符串(例如直到换行符),您可以使用字符串模块中的函数to_integer(String)来评估它:

string:to_integer(String) -> {Int,Rest} | {error,Reason}

There is also the list_to_integer(String) BIF (Built-In Function, just call without a module) but it is not as forgiving as the string:to_integer(String) function:

还有list_to_integer(String)BIF(内置函数,只是在没有模块的情况下调用),但它不像字符串那样宽容:to_integer(String)函数:

list_to_integer(String) -> int()

You will get a badarg exception if the string does not contain an integer.

如果字符串不包含整数,您将获得badarg异常。

#3


If you use string:to_integer/1, check that the value of Rest is the empty list []. The function extracts the integer, if any, from the beginning of the string. It does not assure that the full input converts to an integer.

如果使用string:to_integer / 1,请检查Rest的值是否为空列表[]。该函数从字符串的开头提取整数(如果有)。它不能保证完整输入转换为整数。

string:to_integer(String) -> {Int,Rest} | {error,Reason}

An example:

{Int, Rest} = string:to_integer("33t").
Int.  % -> 33
Rest. % -> "t"

Why check? If the user's finger slipped and hit 't' instead of 5, then the intended input was 335, not 33.

为什么检查?如果用户的手指滑动并且击中't'而不是5,那么预期的输入是335而不是33。

#4


Try printing the number with ~w instead of ~p:

尝试使用~w而不是~p打印数字:

1> io:format("~w~n", [[10]]).
[10]
ok
2> io:format("~p~n", [[10]]).
"\n"
ok

The ~p format specifier tries to figure out whether the list might be a string, but ~w never guesses; it always prints lists as lists.

~p格式说明符试图弄清楚列表是否可能是一个字符串,但~~从不猜测;它总是将列表打印为列表。

#5


Erlang represents strings as lists of integers that are within a certain range. Therefore the input will be a number that represents the character "1" you could subtract an offset to get the actual. Number, sorry don't have a VM here to test a solution.

Erlang将字符串表示为在特定范围内的整数列表。因此输入将是一个表示字符“1”的数字,您可以减去偏移量以获得实际值。不,抱歉这里没有VM来测试解决方案。


推荐阅读
  • 本文讨论了编写可保护的代码的重要性,包括提高代码的可读性、可调试性和直观性。同时介绍了优化代码的方法,如代码格式化、解释函数和提炼函数等。还提到了一些常见的坏代码味道,如不规范的命名、重复代码、过长的函数和参数列表等。最后,介绍了如何处理数据泥团和进行函数重构,以提高代码质量和可维护性。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了[从头学数学]中第101节关于比例的相关问题的研究和修炼过程。主要内容包括[机器小伟]和[工程师阿伟]一起研究比例的相关问题,并给出了一个求比例的函数scale的实现。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文介绍了游标的使用方法,并以一个水果供应商数据库为例进行了说明。首先创建了一个名为fruits的表,包含了水果的id、供应商id、名称和价格等字段。然后使用游标查询了水果的名称和价格,并将结果输出。最后对游标进行了关闭操作。通过本文可以了解到游标在数据库操作中的应用。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文讨论了Kotlin中扩展函数的一些惯用用法以及其合理性。作者认为在某些情况下,定义扩展函数没有意义,但官方的编码约定支持这种方式。文章还介绍了在类之外定义扩展函数的具体用法,并讨论了避免使用扩展函数的边缘情况。作者提出了对于扩展函数的合理性的质疑,并给出了自己的反驳。最后,文章强调了在编写Kotlin代码时可以自由地使用扩展函数的重要性。 ... [详细]
  • 本文介绍了机器学习手册中关于日期和时区操作的重要性以及其在实际应用中的作用。文章以一个故事为背景,描述了学童们面对老先生的教导时的反应,以及上官如在这个过程中的表现。同时,文章也提到了顾慎为对上官如的恨意以及他们之间的矛盾源于早年的结局。最后,文章强调了日期和时区操作在机器学习中的重要性,并指出了其在实际应用中的作用和意义。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • 【shell】网络处理:判断IP是否在网段、两个ip是否同网段、IP地址范围、网段包含关系
    本文介绍了使用shell脚本判断IP是否在同一网段、判断IP地址是否在某个范围内、计算IP地址范围、判断网段之间的包含关系的方法和原理。通过对IP和掩码进行与计算,可以判断两个IP是否在同一网段。同时,还提供了一段用于验证IP地址的正则表达式和判断特殊IP地址的方法。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • EPPlus绘制刻度线的方法及示例代码
    本文介绍了使用EPPlus绘制刻度线的方法,并提供了示例代码。通过ExcelPackage类和List对象,可以实现在Excel中绘制刻度线的功能。具体的方法和示例代码在文章中进行了详细的介绍和演示。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 上图是InnoDB存储引擎的结构。1、缓冲池InnoDB存储引擎是基于磁盘存储的,并将其中的记录按照页的方式进行管理。因此可以看作是基于磁盘的数据库系统。在数据库系统中,由于CPU速度 ... [详细]
author-avatar
小菠萝
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有