看下边的例子,对于不同的表达式,可以用不同的方法去实现,代码可以体会到里边有的不同。 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% filter2( Line )-> Num = re:run(Line, "[0-9]{6}", [{capture,first,list}]), if Num /= nomatch -> {A,[B]} = Num, F = string:substr(B,1,1), if F =:= "0"; F =:= "3"; F =:= "6"-> B; true-> false end; true-> false end. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% getvalue(Key,B) when Key =:= "0"; Key =:= "6"; Key =:= "3"-> B; getvalue(_,_)-> false. filter3( Line )-> case re:run(Line, "[0-9]{6}", [{capture,first,list}]) of nomatch-> false; Value -> {A,[B]} = Value, getvalue( string:left( B, 1 ), B ) end. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% getvalue2(Key,B) when Key =:= "0"; Key =:= "6"; Key =:= "3"-> B; getvalue2(_,_)-> false. guard( A ) when A =:= nomatch-> false; guard( {A, [B] } )-> getvalue2( string:left( B, 1 ), B ). filter4( Line )-> Ret = re:run(Line, "[0-9]{6}", [{capture,first,list}]), guard( Ret ). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% state(X) when X =:= false-> false; state(_) -> true. test8()-> Array = ["fafds","ac002345ae","afsaf600123", "abcef300425"], Ssa = [ filter2(Item) || Item <- Array, filter2(Item) /= false ], Ssb = [ filter3(Item) || Item <- Array, filter3(Item) /= false ], Ssc = [ filter4(Item) || Item <- Array, filter4(Item) /= false ], Fun = fun(X)-> if X =:= false-> false; true-> true end end, Ls2 = lists:filter( Fun, Ssa ), Ls = lists:filter( state, Ssa ), A = 1.
erlang 关于表达式的不同用法。
最新推荐文章于 2022-04-13 16:54:38 发布