erlang list 的使用建议

原文 2014-10-24 19:16:20 发表于 CSDN,这里对以前写的文章做下收录。

erlang有两种复合结构,tuple和list,两者的区别是tuple子元素的个数是固定不变的,声明后就不能改变了;而list是可变的,可以通过[H|T]来取出或插入新元素。上篇文章讲了tuple相关的内容,本篇就讲erlang list方面的知识,主要说一些基本操作和常用的list函数,再讲一些可以优化的点。
list基本操作

1> A = [1, 2, 3, "abc"].
[1,2,3,"abc"]
2> length(A).
4
3> is_list(A).
true
4> list_to_binary(A).
<<1,2,3,97,98,99>>
5> list_to_bitstring(A).
<<1,2,3,97,98,99>>

6> [A1|A2] = A.
[1,2,3,"abc"]
7> A1.
1
8> A2.
[2,3,"abc"]
9> hd(A).
1
10> tl(A).
[2,3,"abc"]
11> B = [0|A].
[0,1,2,3,"abc"]

继续阅读erlang list 的使用建议

C++ regex 正则表达式

原文 2014-02-09 19:32:01 发表于 CSDN,这里对以前写的文章做下收录。

在c++中,有三种正则可以选择使用,C ++regex,C regex,boost regex ,如果在windows下开发c++,默认不支持后面两种正则,如果想快速应用,显然C++ regex 比较方便使用。文章将讨论 C++ regex 正则表达式的使用。

C++ regex函数有3个:regex_match、 regex_search 、regex_replace

regex_match

regex_match是正则表达式匹配的函数,下面以例子说明。如果想系统的了解,参考regex_match
继续阅读C++ regex 正则表达式

erlang catch 异常处理

原文 2015-03-31 01:16:10 发表于 CSDN,这里对以前写的文章做下收录。

在说 erlang 异常捕获处理(catch)前,不得不说 erlang 内部数据(Eterm)。Eterm 是 Erlang Term 的简写,用来表示 erlang 中任意类型的数据,也就是说,erlang 可以用到的任意数据,都能 Eterm 表示。比如常见的 atom、数字、列表、元组,甚至 pid,port,fun,ets 表等等都用Eterm可以表示。 

Eterm

Eterm 在 VM 中主要分为三大类:列表,boxed 对象,立即数。(这么分法主要是复杂的数据无法单靠1个机器字表示,在32位机器上,一个字长4字节,在64位机器上是8字节。)

继续阅读erlang catch 异常处理

erlang时间校正机制

原文 2015-04-29 00:34:06 发表于 CSDN,这里对以前写的文章做下收录。

很多人会注意到这个问题,erlang提供了2个时间函数,erlang:now() 和 os:timestamp()。用法一样,都是返回当前的时间。具体时间是从1970年1月1日零时算起,到现在经过的时间,结果为{MegaSecs, Secs, MicroSecs}。

这两个函数有什么区别?
os:timestamp() 获取到的时间为操作系统的时间,不做任何修正;而erlang:now(),每次获取都会确保生成了唯一的时间,就是说,erlang:now()在实现上对时间做了一个校正,每次都生成一个单调向前的唯一值。

erlang:now()的特点:

Monotonic
erlang:now() never jumps backwards - it always moves forward
Interval correct
The interval between two erlang:now() calls is expected to correspond to the correct time in real life (as defined by an atomic clock, or better)
Absolute correctness
The erlang:now/0 value should be possible to convert to an absolute and correct date-time, corresponding to the real world date and time (the wall clock)
System correspondence
The erlang:now/0 value converted to a date-time is expected to correspond to times given by other programs on the system (or by functions like os:timestamp/0)
Unique
No two calls to erlang:now on one Erlang node should return the same value
继续阅读erlang时间校正机制

C/C++ 分割字符串的多种方式

原文 2013-11-09 16:20:40 发表于 CSDN,这里对以前写的文章做下收录。

strtok函数
在C/C++中, strtok函数被用来拆分字符串
strtok函数原型:
char *strtok( char *str, const char *delims );

strtok函数例子:

char str[] = "now#the tiger is coming#please run away";
char delims[] = "#";
char *result = NULL;

result = strtok( str, delims );
while( result != NULL )
{
   printf( "result is \"%s\"\n", result );
   result = strtok( NULL, delims );
}

但是如何二次拆分字符串,比如说分解GET参数?这时候就需要使用strtok的线程安全版本strtok_s(linux下为strtok_r)。
继续阅读C/C++ 分割字符串的多种方式

erlang 接入远程shell控制台

erlang shell是用户与 erlang 运行时系统交互的界面程序。事实上,erlang VM的运行不依赖任何shell,只要在启动的时候添加参数detached就可以脱离终端。
-detached
Starts the Erlang runtime system detached from the system console. Useful for running daemons and backgrounds processes. Implies -noinput.
实际上,detached 等效于noshell 加上 noinput。
# erl -detached -emu_args
Executing: /home/erl/lib/erlang/erts-5.10.3/bin/beam /home/erl/lib/erlang/erts-5.10.3/bin/beam -- -root /home/erl/lib/erlang -progname erl -- -home /root -- -noshell -noinput
另外,需要注意的是,windows不直接支持detached,而是以服务的形式在后台运行,见 erlsrv

现在讨论erlang 接入远程shell控制台的几种方式。
继续阅读erlang 接入远程shell控制台