`
acme_ltt
  • 浏览: 52233 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

Erlang 实践杂记-1

阅读更多

 

一、列表、字符串相关

 

 

1、场景描述

当下,Erlangphp交互,用mochiwebibrowse的方式比较多,ibrowse 需要发送urlphpurl为字符串。若需要string:concat() 的变量不为string类型,就需要进行转化。前提是,Erlang处理中,多为二进制。

2、问题

1> B = <<"test">>.

<<"test">>

2> S = binary_to_term(B).

** exception error: bad argument

     in function  binary_to_term/1

        called as binary_to_term(<<"test">>)

 

上例,视图将binary类型的数据转换成term数据,出现 bad argument 错误,悲剧。

 

3> L = binary_to_list(B).

"test"

貌似是可以了,但是,使用string:concat("my", L) 时会再一次发生悲剧,这个时候的Llist类型,而string:concat 要求的参数是string类型,所以当然会悲剧。

 

3、解决

1> B = <<"test">>.

<<"test">>

2> L = binary_to_list(B).

"test"

3> S = lists:flatten(L).

"test"

 

这个时候,再去看看string:concat("my", S) OK,搞定。

 

4、总结

    http://www.erlang.org/doc/man/lists.html#flatten-1 

    flatten(DeepList) -> List

     Types:

DeepList = [term() | DeepList]

        List = [term()]

    Returns a flattened version of DeepList.

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics