Related
I have a list like this one ['a','b','c','d'] and what I need is to add a affix to each item in that list like : ['a#erlang','b#erlang','c#erlang','d#erlang']
I tried using 1lists:foreach1 and then concat two strings to one and then lists:append to the main list, but that didn't work for me.
Example of what I tried:
LISTa = [],
lists:foreach(fun (Item) ->
LISTa = lists:append([Item,<<"#erlang">>])
end,['a','b','c','d'])
Thanks in advance.
1> L = ['a','b','c','d'].
[a,b,c,d]
2> [ list_to_atom(atom_to_list(X) ++ "#erlang") ||X <- L].
[a#erlang,b#erlang,c#erlang,d#erlang]
Please try this code, you can use list_to_atom and atom_to_list.
This will do the trick (using list comprehensions):
1> L = ["a","b","c","d"].
["a","b","c","d"]
2> R = [X ++ "#erlang" || X <- L].
["a#erlang","b#erlang","c#erlang","d#erlang"]
3>
Notice that I changed the atoms for strings; It's discouraged to "create atoms on the fly/dynamically" in Erlang, so I have that framed in my mind. If you still need so, change the implementation a little bit and you are good to go.
NOTE: I'm assuming the concatenation between atoms and binaries is, somehow, something you did not do on purpose.
I am going through the tutorials of riak and Erlang, I have stored data in riak by the riak-erlang-client and i did the following:
1> {ok,Pid} = riakc_pb_socket:start_link("127.0.0.1", 8087).
{ok,<0.34.0>}
2> Val1 = [1,2,3].
[1,2,3]
3> MyBucket = <<"numbers">>.
<<"numbers">>
4> Obj1 = riakc_obj:new(MyBucket,<<"ott">>,Val1).
5> riakc_pb_socket:put(Pid,Obj1).
ok
6> {ok, Fetched} = riakc_pb_socket:get(Pid,MyBucket,<<"ott">>).
{ok,{riakc_obj,<<"numbers">>,<<"ott">>,
<<107,206,97,96,96,96,204,96,202,5,82,28,202,156,255,126,
6,251,159,184,148,193,148,...>>,
[{{dict,3,16,16,8,80,48,
{[],[],[],[],[],[],[],[],[],[],[],[],...},
{{[],[],[],[],[],[],[],[],[],[],...}}},
<<131,107,0,3,1,2,3>>}],
undefined,undefined}}
7> Val1 == binary_to_term(riakc_obj:get_value(Fetched)).
true
8> binary_to_term(riakc_obj:get_value(Fetched)).
[1,2,3]
Now everything was fine , i want to extract the values from the list [1,2,3] that where only divisible by 2 from a mapreduce through erlang.
for that i have done the following,
9>Res = fun(Fetched,none)-> [X || X <- binary_to_term(riakc_obj:get_value(Fetched)), X rem 2 == 0] end.
I don't know exactly about what should write in the function parameters,as i am new to erlang and riak, so please correct me if am wrong.
Now when i going for mapreduce i have written this,
10>{ok , [{X}]} = riakc_pb_socket:mapred(Pid,[{<<"numbers">>,<<"ott">>}],[{map,{qfun,Res},none,true}]).
It was throwing the below exception..please suggest me a good way to understand what's going on and how to run mapreduce.
** exception exit: {noproc,{gen_server,call,
[<0.34.0>,
{req,{rpbmapredreq,<<131,108,0,0,0,3,104,2,100,0,6,105,
110,112,117,116,115,108,...>>,
<<"application/x-erlang-binary">>},
60100,
{132578423,<0.49.0>}},
infinity]}}
in function gen_server:call/3 (gen_server.erl, line 188)
in call from riakc_pb_socket:mapred/5 (src/riakc_pb_socket.erl, line 643)
Please,help me and suggest me, i hjave followed the links but i am not getting clearly that how to write on my own for a mapreduce so please guide me..it will help me.
You are getting the noproc error because you are passing an arity-2 function. The map phase function should be arity-3. Also, specifying none in the function header will cause that clause to only be executed when the corresponding argument explicitly matches, use _ in place of arguments you don't care about, like:
Res = fun(Fetched,_,_)-> [X || X <- binary_to_term(riakc_obj:get_value(Fetched)), X rem 2 == 0] end.
I work in erlang
Now, I have a big problem
I want to have a log from a table mnesia and this log should be write in excel file
So the goal is write data from table mnesia to the excel file
I think and this is related to some code find in this forum that the best way is to write .txt file then transfer data from .txt file to excel file
I find this code in this forum in this link.
exporttxt()->
F = fun(T) -> mnesia:foldl(fun(X,Acc) -> [X|Acc] end, [],T) end,
{atomic,L} = mnesia:transaction(F(user)),
file:write_file("test.txt",[io_lib:format("~p\t~p\t~p~n",[F1,F2,F3]) ||
#user{id = F1,adress = F2,birthday = F3} <- L]).
But this code produces an error
As commented, the problem is clearly explained in the link itself. If you want the code then here it is. But please understand before directly jumping into the code.
exporttxt()->
F = fun() -> mnesia:foldl(fun(X,Acc) -> [X|Acc] end, [],user) end,
{atomic,L} = mnesia:transaction(F),
file:write_file("test.txt",[io_lib:format("~p\t~p\t~p~n",[F1,F2,F3]) ||
#user{id = F1,adress = F2,birthday = F3} <- L]).
in the subject you mention, I said that I didn't test the code, and of course there was a syntax error.
Here is a code that run.
1> ok = mnesia:create_schema([node()]).
ok
2> rd(my_user,{firstname,lastname,age}).
my_user
3> ok =application:start(mnesia).
ok
4> {atomic,ok} = mnesia:create_table(my_user,[{attributes,record_info(fields,my_user)},{disc_copies,[node()]},{type,bag}]).
{atomic,ok}
5> Add_user = fun(Fn,Ln,Ag) ->
5> F = fun() -> mnesia:write(my_user,#my_user{firstname=Fn,lastname=Ln,age=Ag},write) end,
5> mnesia:activity(transaction,F)
5> end.
#Fun<erl_eval.18.82930912>
6> ok = Add_user("Georges","Boy",25).
ok
7> ok = Add_user("Joe","Armstrong",52).
ok
8> ok = Add_user("Pascal","Me",55).
ok
9> F = fun(T) -> mnesia:foldl(fun(X,Acc) -> [X|Acc] end, [],T) end.
#Fun<erl_eval.6.82930912>
10> {atomic,L} = mnesia:transaction(F,[my_user]).
{atomic,[#my_user{firstname = "Pascal",lastname = "Me",
age = 55},
#my_user{firstname = "Joe",lastname = "Armstrong",age = 52},
#my_user{firstname = "Georges",lastname = "Boy",age = 25}]}
11> ok = file:write_file("test.txt",[io_lib:format("~p\t~p\t~p~n",[F1,F2,F3]) || #my_user{firstname = F1, lastname = F2, age = F3} <- L]).
ok
12>
you will have in your working directory a file named test.txt containing
"Pascal" "Me" 55
"Joe" "Armstrong" 52
"Georges" "Boy" 25
and if you open it with excel you will get
But this is not a code sequence you should use directly.
line 1 should take place in a code use for the deployment of your application.
line 2 is a record definition, necessary for the shell to understand the next lines. It should be replaced by a -record(...) in a module or an included file.
lines 3 and 4 should take place in the init function of one of the higher level supervisor (with some test to check already started application, existing table...)
line 5 should be in the interface definition of a server
line 6,7,8 should be generated by a user interface in some client
and last line 9,10,11 in another interface (for admin?).
Message to Lost_with_coding and his fellows,
If I may give you my opinion, you are burning steps. You should focus on mastering the Erlang syntax, the concept of pattern matching and variable bounding... after you should look at more advance construction such as list and binary comprehensions. Take time to look at error messages and use them to solve simple problems. The official Erlang documentation is great for this purpose. I always have it open in my browser and sometimes I also use this link http://erldocs.com/R15B/ when I look for function I don't know.
Next will come higher order functions, error handling, processes, concurrency, OTP... plus the usage of the efficient but not sexy Erlang tools (tv, appmon, debugger...).
I recommend it very often, but use the fantastic Fred Hebert's web site http://learnyousomeerlang.com/ and follow it step by step, rewriting the code, not copy/paste; it really worths the effort.
got a problem with dets:to_ets/2
Could somebody point me to an example online? Ive looked at the man pages but I couldnt see any example usage. Couldn't find anything on google..
My problem seems to be with the actual dets:to_ets() function itself, not the creating of the dets. I have tested that on it's own and it's fine.
You should create ETS table before use to_ets/2 function on it. The existing objects of the Ets table are kept unless overwritten. Do you have any {error, Reason} tuples in the result?
a quick example of dets:to_ets/2.
1> dets:open_file(d, [{file, "/tmp/d"}, {type, set}]).
{ok,d}
2> dets:insert(d, {a, 1}).
ok
3> dets:insert(d, {b, 2}).
ok
4> ets:new(e, [named_table, set]).
e
5> dets:to_ets(d, e).
e
6> ets:tab2list(e).
[{b,2},{a,1}]
i was wondering if anyone could help me drawing the syntactic tree of a very very simple instruction in erlang : a simple assignment like A = 2 + 3. using , of course the erlang official grammar available at http://svn.ulf.wiger.net/indent/trunk/erl_parse.yrl
Thanks for everything
You can simply use Erlang own tools:
1> {ok, Toks, _} = erl_scan:string("A=2+3.").
{ok,[{var,1,'A'},
{'=',1},
{integer,1,2},
{'+',1},
{integer,1,3},
{dot,1}],
1}
2> {ok, [AST]} = erl_parse:parse_exprs(Toks).
{ok,[{match,1,
{var,1,'A'},
{op,1,'+',{integer,1,2},{integer,1,3}}}]}
3> AST.
{match,1,{var,1,'A'},{op,1,'+',{integer,1,2},{integer,1,3}}}