• Erlang入门——将字符串去““转为普通类型


    前言:要不是种种原因,搞java的我为何会转这门小语言,真的难受。Erlang基础语法不难上手,函数式编程的思维和以前所学的不太一样。难在于,社区太小了,遇到不会的找不到他人经验,找人问都没地方问…

    作为学习java和c过来的人,对社区经验分享与学习深有体会,反正只要我能留下来继续搞Erlang,自己碰到的问题以及琢磨出的解决办法都会分享。

    步入正题。

    (服务端用gen_tcp:listen/2打开端口生成Socket,gen_tcp:accept/2响应来自客户端gen_tcp:connect/2的连接,这个书上或官方文档有就不提了)

    在分布式编程场景中,通过gen_tcp跨节点传值,设置了端口参数,类型为binary(二进制数据),{pakcet, 4}(收到的数据去掉头部)等等

    ...
    -define(TCP_OPTIONS, [binary, {packet, 4}, {active, true}, {reuseaddr, true}]).
    
    start(Port) ->
    	{ok, LSocket} = gen_tcp:listen(Port, ?TCP_OPTIONS),
    ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    服务端接收到的数据用binary_to_term转换后是字符串形式(如果是可打印字符shell会转换成字符串,否则就是ASCII码的列表形式)。在Erlang里,字符串就是装着ASCII码的列表。然而字符串形式的数值用不了(至少我现在刚学不知怎么用),这时就要转换或提取字符串了。

    核心方法:
    erl_scan:string/1
    erl_parse:parse_exprs/1
    erl_eval:exprs/2

    receive
    		{tcp, Socket, Bin} ->
    			Str = binary_to_term(Bin),
    			Str1 = lists:subtract(Str, "\n"),
    			
    			%%字符串去""转为列表
    			{ok, Scan1, _} = erl_scan:string(Str1 ++ "."),
    			{ok, Scan2} = erl_parse:parse_exprs(Scan1),
    			{_, Value, _} = erl_eval:exprs(Scan2, []),
    			{X, Y} = Value,
    			
    			io:format("Server get the X: ~p ~n", [X]),
    			io:format("Server get the Y: ~p ~n", [Y]),
    			...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    假设客户端输入 {4, 5} 传值过来
    如果在每个操作之间进行打印

    {tcp, Socket, Bin} ->
    			io:format("Point C ~n"),
    			Str2 = bitstring_to_list(Bin),
    			io:format("Server get msg: ~p ~n", [Str2]),
    			io:format("What is the type of the msg : ~p ~n", [is_atom(Str2)]),
    			io:format("What is the type of the msg : ~p ~n", [is_list(Str2)]),
    			io:format("What is the type of the msg : ~p ~n", [is_tuple(Str2)]),
    			io:format("What is the type of the msg : ~p ~n", [is_bitstring(Str2)]),
    			io:format("What is the type of the msg : ~p ~n", [is_reference(Str2)]),
    			Str = binary_to_term(Bin),
    			io:format("Server get msg: ~p ~n", [Str]),
    			io:format("What is the type of the msg : ~p ~n", [is_atom(Str)]),
    			io:format("What is the type of the msg : ~p ~n", [is_list(Str)]),
    			io:format("What is the type of the msg : ~p ~n", [is_tuple(Str)]),
    			io:format("What is the type of the msg : ~p ~n", [is_bitstring(Str)]),
    			io:format("What is the type of the msg : ~p ~n", [is_reference(Str)]),
    			Str1 = lists:subtract(Str, "\n"),
    			io:format("Server get msg: ~p ~n", [Str1]),
    			%%{X, Y} = io:parse_erl_form(Str1),
    			%%{X, Y} = io:scan_erl_form(Str1),
    			
    			%%字符串去""转为列表
    			{ok, Scan1, _} = erl_scan:string(Str1 ++ "."),
    			{ok, Scan2} = erl_parse:parse_exprs(Scan1),
    			{_, Value, _} = erl_eval:exprs(Scan2, []),
    			io:format("Server get the Value: ~p ~n", [Value]),
    			%%{X, Y} = list_to_tuple(Value),
    			{X, Y} = Value,
    			
    			io:format("Server get the X: ~p ~n", [X]),
    			io:format("Server get the Y: ~p ~n", [Y]),
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    输出效果是:
    在这里插入图片描述
    可见去掉""后,内部是什么类型的数据就会转为相应类型,如例子里是{4,5},为tuple类型。

    参考自:
    https://blog.csdn.net/weixin_30653097/article/details/94898270?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_title~default-0-94898270-blog-89374488.pc_relevant_show_downloadRating&spm=1001.2101.3001.4242.1&utm_relevant_index=3

    https://www.cnblogs.com/daofen/p/6047789.html

    小白入门,边学边分享。如有错误,敬请指正~

  • 相关阅读:
    zabbix6.0监控磁盘IO
    在关系型数据库中储存树形结构
    元宇宙的宏观与微观趋势
    mindspore.dataset.NumpySlicesDataset使用报错
    【无标题】
    Day 44 Ansible自动化运维
    Java多线程学习入门(五):ThreadLocal以及对象内存布局
    react antdesign table 添加滚动加载(下拉翻页功能)
    2、图机器学习——Graph Embedding
    gin 集成 Swagger
  • 原文地址:https://blog.csdn.net/weixin_42549874/article/details/126092256