一、列表、字符串相关
1、场景描述
当下,Erlang和php交互,用mochiweb和ibrowse的方式比较多,ibrowse 需要发送url给php,url为字符串。若需要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) 时会再一次发生悲剧,这个时候的L是list类型,而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.