避免将数字转换为erlang中的字符 [英] Avoid converting numbers to characters in erlang

查看:105
本文介绍了避免将数字转换为erlang中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只要没有列出的项目也不能代表一个字符,我将erlang转换为字符时遇到麻烦。



我正在编写一个函数来分隔正整数中的所有数字,并将它们放在列表中,例如: digitalize(123)应该返回 [1,2,3] 等等。



以下代码工作正常,除非列表仅包含8和/或9的:



数字化(_N)_N =:= 0 - > [];
数字化(_N)_N> 0 - > _H = [_N rem 10],_T = digitalize(_N div 10),_T ++ _H。



例如: strong>数字化(8)返回 [8] ,它给我非常字符\b数字化(89) strong>返回\b\t。这只适用于数字8和9,当它们放在列表中时。 数字化(891)将正确返回 [8,9,1]



我知道这样做的原因,但是如何解决它而不改变我的结果? (例如:在数字化(8))内的结果中包含空列表 [[],[8]]

解决方案

如果您查看评论,您会发现shell打印数据的方式比数据本身更多的问题。你的逻辑是可怕的,我不会改变它。您可以向您的代码介绍一些使用 io:format / 2 ,但我猜这将使得在代码的其他部分使用此功能变得更加困难。 >

其他方法是更改​​shell设置本身。有 shell:stings / 1 函数禁用打印列表作为蜇,它应该完全符合你想要的。只要记住当你完成你的shell的东西时,改回它,因为当你开始使用一些string返回函数时,它可能会引起一些混乱。


I'm having trouble with erlang converting listed numbers to characters whenever none of the listed items could not also be representing a character.

I am writing a function to separate all the numbers in a positive integer and put them in a list, for example: digitize(123) should return [1,2,3] and so on.

The following code works fine, except when the list only consist of 8's and/or 9's:

digitize(_N) when _N =:= 0 -> []; digitize(_N) when _N > 0 -> _H = [_N rem 10], _T = digitize(_N div 10), _T ++ _H.

For example: Instead of digitize(8) returning [8], it gives me the nongraphic character "\b" and digitize(89) returns "\b\t". This is only for numbers 8 and 9 and when they're put alone inside the list. digitize(891) will correctly return [8,9,1] for example.

I am aware of the reason for this but how can I solve it without altering my result? (ex: to contain empty lists inside the result like [[],[8]] for digitize(8))

解决方案

If you look at comments you will see that it is more problem of the way shell prints your data, than the data itself. You logic is wright and I would not change it. You could introduce some use of io:format/2 into you code, but I guess that it would make it harder to use this function in other parts of code.

Other way around it is changing the shell settings itself. There is shell:stings/1 functions that disables printing lists as stings, and it should do exactly what you want. Just remember to change it back when you finish with your shell stuff, since it could introduce some confusion when you will start using some "string" returning functions.

这篇关于避免将数字转换为erlang中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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