Erlang中的字符串分割问题
我一直在研究原子分裂,并且对弦有疑问。输入数据始终是由一些字母和一些数字组成的原子,例如 ms444
、r64
或 min1
。由于函数 lists:splitwith/2
接受一个列表,原子首先被转换为一个列表:
24> lists:splitwith(fun (C) -> is_atom(C) end, [m,s,4,4,4]).
{[m,s],[4,4,4]}
25> lists:splitwith(fun (C) -> is_atom(C) end, atom_to_list(ms444)).
{[],"ms444"}
26> atom_to_list(ms444).
"ms444"
我想将字母与数字分开,并且在使用列表时我已经成功地做到了这一点,但是因为我从一个原子开始,所以我得到一个“字符串”作为结果放入我的 splitwith 函数中......
它是将列表中的每个项目解释为字符串还是发生了什么?
I've been playing around with the splitting of atoms and have a problem with strings. The input data will always be an atom that consists of some letters and then some numbers, for instance ms444
, r64
or min1
. Since the function lists:splitwith/2
takes a list the atom is first converted into a list:
24> lists:splitwith(fun (C) -> is_atom(C) end, [m,s,4,4,4]).
{[m,s],[4,4,4]}
25> lists:splitwith(fun (C) -> is_atom(C) end, atom_to_list(ms444)).
{[],"ms444"}
26> atom_to_list(ms444).
"ms444"
I want to separate the letters from the numbers and I've succeeded in doing that when using a list, but since I start out with an atom I get a "string" as result to put into my splitwith
function...
Is it interpreting each item in the list as a string or what is going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能想查看字符串模块文档:
http://www.erlang。 org/doc/man/string.html
您可能会对以下函数感兴趣:
You might want to have a look at the string module documentation:
http://www.erlang.org/doc/man/string.html
The following function might interest you:
由于 Erlang 中的字符串只是
integer()
的list()
,因此如果该项目是atom()
,则将在 fun 中进行测试> 当它实际上是一个integer()
时。如果将测试更改为查找字母,则它会起作用:Since strings in Erlang are just a
list()
ofinteger()
the test in the fun will be made if the item is anatom()
when it is in fact aninteger()
. If the test is changed to look for letters it works:erlang 中的原子是一个命名常量,而不是一个变量(或者不像命令式语言中的变量)。
您确实不应该以动态方式创建原子(即,不要在运行时将事物转换为原子)
它们更多地用于模式匹配和发送接收代码。
变量(例如 X)可以设置为原子。例如 X=if 1==1 ->好的;正确->失败结束。我可能会缺乏想象力,但我想不出为什么你想解析原子。您应该负责编写哪些原子,而不是使用
list_to_atom
(CharIntegerList)。您能否更详细地概述一下您想要完成的任务?
An atom in erlang is a named constant and not a variable (or not like a variable is in an imperative language).
You should really not create atoms in dynamic fashion (that is, don't convert things to atoms at runtime)
They are used more in pattern matching and send recive code.
A variable, like X could be set to an atom. For example X=if 1==1 -> ok; true -> fail end. I could suffer from poor imagination but I can't think of a way why you would like to parse atom. You should be in charge of what atoms you write and not use
list_to_atom
(CharIntegerList).Can you perhaps give a more overview of what you like to accomplish?
Erlang 中的“字符串”不是原始类型:它只是整数()的列表()。因此,如果您想将字母与数字“分开”,则必须与字符的整数表示进行比较。
A "string" in Erlang is not a primitive type: it is just a list() of integers(). So if you want to "separate" the letters from the digits, you'll have to do comparison with the integer representation of the characters.