Erlang中的字符串分割问题

发布于 2024-08-10 13:55:19 字数 525 浏览 2 评论 0原文

我一直在研究原子分裂,并且对弦有疑问。输入数据始终是由一些字母和一些数字组成的原子,例如 ms444r64min1。由于函数 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜吻♂芭芘 2024-08-17 13:55:19

您可能想查看字符串模块文档:

http://www.erlang。 org/doc/man/string.html

您可能会对以下函数感兴趣:

tokens(String, SeparatorList) -> Tokens

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:

tokens(String, SeparatorList) -> Tokens
超可爱的懒熊 2024-08-17 13:55:19

由于 Erlang 中的字符串只是 integer()list(),因此如果该项目是 atom(),则将在 fun 中进行测试> 当它实际上是一个integer()时。如果将测试更改为查找字母,则它会起作用:

29> lists:splitwith(fun (C) -> (C >= $a) and (C =< $Z)  end, atom_to_list(ms444)).
{"ms","444"}

Since strings in Erlang are just a list() of integer() the test in the fun will be made if the item is an atom() when it is in fact an integer(). If the test is changed to look for letters it works:

29> lists:splitwith(fun (C) -> (C >= $a) and (C =< $Z)  end, atom_to_list(ms444)).
{"ms","444"}
や莫失莫忘 2024-08-17 13:55:19

erlang 中的原子是一个命名常量,而不是一个变量(或者不像命令式语言中的变量)。
您确实不应该以动态方式创建原子(即,不要在运行时将事物转换为原子)

它们更多地用于模式匹配和发送接收代码。

Pid ! {matchthis, X}


recive
{foobar,Y} -> doY(Y);
{matchthis,X} -> doX(X);
Other -> doother(Other)
end

变量(例如 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.

Pid ! {matchthis, X}


recive
{foobar,Y} -> doY(Y);
{matchthis,X} -> doX(X);
Other -> doother(Other)
end

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?

维持三分热 2024-08-17 13:55:19

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文