Erlang exited with reason - undef (with string:find/2 func) - erlang

trying to use function string:find/2but every time getting the error
CRASH REPORT Process <0.779.0> with 0 neighbours exited with reason: {{undef,[{string,find,[[<<208,162,51,32,208,190,208,177,209,137,46,44,32,84,51,32,116,111,116,97,108,44>>],[<<208,186,209,128,208,190,208,178>>]],[]},{proxy_layer_cli_handle_req,do_execute_keysearch,4,[{file,\"/opt/proxy_layer/_build/test/lib/proxy_layer/src/proxy_layer_cli_handle_req.erl\"},{line,222}]},{proxy_layer_cli_handle_req,keysearch,3,[{file,\"/opt/proxy_layer/_build/test/lib/proxy_layer/src/proxy_layer_cli_handle_req.erl\"},{line,...}]},...]},...}
when i use it in terminal - everything is okay
1> string:find(<<208,162,51,32,208,190,208,177,209,137,46,44,32,84,51,32,116,111,116,97,108,44>>,<<208,186,209,128,208,190,208,178>>).
1> nomatch
I'm using Erlang 20.1
here is the code which I use:
do_execute_keysearch([First|Rest], PriceList, Keyword, Acc) ->
Id = utils:get_value(<<"Id">>, First),
case utils:get_value(<<"Keywords">>, First) of
<<>> -> do_execute_keysearch(Rest, PriceList, Keyword, Acc);
undefined -> do_execute_keysearch(Rest, PriceList, Keyword, Acc);
Keys ->
case string:find(Keys, Keyword) of
nomatch ->
do_execute_keysearch(Rest, PriceList, Keyword, Acc);
_ ->
Price = find_price_by_service_id(PriceList, Id),
NewAcc = [lists:append(Price, First) | Acc],
do_execute_keysearch(Rest, PriceList, Keyword, NewAcc)
end
end;
UPDATE:
Issue fixed after changing Erlang version in docker container. (Changed to Erlang 20.1)
Don’t know why there are some modules undefined in Erlang 19
So the problem solved now

string:find/2 was added to Erlang in version 20 which is why you were getting an undef error in Erlang 19. The solution is to upgrade to Erlang 20 (which you've already done).

Look at the error more carefully. Or rather, try to reproduce it. Which of these looks more like your error?
1> catch lists:nonexist(<<1>>, <<2>>).
{'EXIT',{undef,[{lists,nonexist,[<<1>>,<<2>>],[]},
{erl_eval,do_apply,6,[{file,"erl_eval.erl"},{line,674}]},
{erl_eval,expr,5,[{file,"erl_eval.erl"},{line,431}]},
{shell,exprs,7,[{file,"shell.erl"},{line,687}]},
{shell,eval_exprs,7,[{file,"shell.erl"},{line,642}]},
{shell,eval_loop,3,[{file,"shell.erl"},{line,627}]}]}}
2> catch lists:nonexist([<<1>>], [<<2>>]).
{'EXIT',{undef,[{lists,nonexist,[[<<1>>],[<<2>>]],[]},
{erl_eval,do_apply,6,[{file,"erl_eval.erl"},{line,674}]},
{erl_eval,expr,5,[{file,"erl_eval.erl"},{line,431}]},
{shell,exprs,7,[{file,"shell.erl"},{line,687}]},
{shell,eval_exprs,7,[{file,"shell.erl"},{line,642}]},
{shell,eval_loop,3,[{file,"shell.erl"},{line,627}]}]}}

Related

External function call Erlang

I'm trying to call a function (from an external module) in erlang. both beam files are located in the same directory.
-module(drop2).
-export([fall_velocity/1]).
fall_velocity(Distance) -> math:sqrt(2 * 9.8 * Distance).
Then I'm calling
-module(ask).
-export([term/0]).
term() ->
Input = io:read("Enter {x,distance} ? >>"),
Term = element(2,Input),
drop2:fall_velocity(Term).
it gives the following error.I tested individual modules for errors. it is compiling with out any errors or warning.
Eshell V5.10.2 (abort with ^G)
1> ask:term().
Enter {x,distance} ? >>{test,10}.
** exception error: an error occurred when evaluating an arithmetic expression
in function drop2:fall_velocity/1 (drop2.erl, line 3)
Not sure why it is throwing arithmetic expression error .
You could read the documentation to figure out that the result is {ok, Term}. You could try the io:read/1 function in the console, then you'd see following:
1> io:read("Enter > ").
Enter > {test, 42}.
{ok,{test,42}}
2>
That means that you need to deconstruct the result of io:read/1 differently, for example like this:
-module(ask).
-export([term/0]).
term() ->
{ok, {_, Distance}} = io:read("Enter {x, distance} > "),
drop2:fall_velocity(Distance).

Having Dialyzer support Custom Behaviours

I am using Dialyzer with a few custom behaviors, the problem is that when I do that, Dialyzer gives me this error:
src/max.erl:3: Callback info about the gen_strategy behaviour is not available
One thing I can't figure out is how to create that callback info. I would like to add this information to my behaviour, so I can get that much more testing out of Dialyzer.
Starting with R15B, The Erlang/OTP compiler was upgraded so that it now handles a new module attribute, named -callback.
Example:
-callback init(Args :: term()) ->
{ok, State :: term()} | {ok, State :: term(), timeout() | hibernate} |
{stop, Reason :: term()} | ignore.
More about that here and here

Erlang rr() returns missing_chunk

Newbie question on erlang using by wings 3d shell (windows 7 pro, wings 3d 1.4.1). When I write command to read records definitons:
rr(wings).
I always get an error:
{error,beam_lib,
{missing_chunk,'d:/temp/erlang/lib/wings/ebin/wings.beam',"Abst"}}
What I am doing wrong?
rr/1 "reads record definitions from a module's BEAM file. If there are no record definitions in the BEAM file, the source file is located and read instead."
My guess is that the abstract form hasn't been included in the .BEAM file and that in your installation the source files are not available.
UPDATE: Digging into the shell:read_file_records/2 function, I've found the following:
read_file_records(File, Opts) ->
case filename:extension(File) of
".beam" ->
case beam_lib:chunks(File, [abstract_code,"CInf"]) of
{ok,{_Mod,[{abstract_code,{Version,Forms}},{"CInf",CB}]}} ->
case record_attrs(Forms) of
[] when Version =:= raw_abstract_v1 ->
[];
[] ->
%% If the version is raw_X, then this test
%% is unnecessary.
try_source(File, CB);
Records ->
Records
end;
{ok,{_Mod,[{abstract_code,no_abstract_code},{"CInf",CB}]}} ->
try_source(File, CB);
Error ->
%% Could be that the "Abst" chunk is missing (pre R6).
Error
end;
_ ->
parse_file(File, Opts)
end.
It looks like, if the "Abst" chunk is missing, it doesn't even try to read the source code. What does beam_lib:chunks(File, [abstract_code,"CInf"]) return for you? Which Erlang version are you running?
I am using:
Erlang R14B01 (erts-5.8.2) [rq:1] [async-threads:0]
Eshell V5.8.2 (abort with ^G)
Calling this works fine:
rr("d:/temp/erlang/src/wings.hrl").
Calling:
beam_lib:chunks("wings", [abstract_code,"CInf"]).
returns:
{error,beam_lib,{file_error,"wings.beam",enoent}}
rr/1 requires the actual file name or the relative PATH to the file name. Like this:
rr("wings.hrl").
OR
rr("./apps/MYAPP-1.0/include/my_include_file.hrl").
OR
rr("./src/wings.erl").
In other words, pass it the actual relative PATH from your pwd() to the file which contains the definitions.

Erlang runtime error

I'm working on an erlang program and getting a strange runtime error. Any idea why? Thanks!
The errors are (after compiling the program successfully):
8> PID = spawn(planner,start,[]).
** exception error: no match of right hand side value <0.65.0>
9>
This is the program:
-module(planner).
-export([start/0]).
start() ->
loop([],[]).
loop(ContactsList,EventsList) ->
receive
{contact, Last, First, Number} ->
loop([{Last,First,Number}|ContactsList],EventsList);
{event, Date, Time, What} ->
loop([{Date,Time,What}|ContactsList],EventsList);
print_contacts ->
NewList=lists:sort(ContactsList),
lists:foreach(fun(Elem)->io:format("~p~n", [Elem]) end, NewList),
loop(ContactsList,EventsList);
print_events ->
NewList=lists:sort(EventsList),
lists:foreach(fun(Elem)->io:format("~p~n", [Elem]) end, NewList),
loop(ContactsList,EventsList);
exit ->
io:format("Exiting.~n");
_ ->
io:format("Dude, I don't even know what you're talking about.~n"),
loop(ContactsList,EventsList)
end.
The variable PID is probably set to something else but <0.65.0> from an earlier line you entered in the shell:
5> PID = spawn(...).
<0.42.0>
8> PID = spawn(...).
** exception error: no match of right hand side value <0.65.0>
This effectively makes the line which generates the error something like
8> <0.42.0> = <0.65.0>.
which will result in a "no match" error.
More obvious illustration of the issue:
1> X = 1.
1
2> X = 2.
** exception error: no match of right hand side value 2
As to solving your issue: You probably want to run f(PID) to have the shell forget just the PID variable, or even f() to have the shell forget ALL variables.

Avoid NZEC error in Erlang in SPOJ

I've written code in Erlang, and I get the correct answer on my machine. But when I submit it on SPOJ it gives an NZEC (non zero exit code) error. I have used built-in functions like halt() and init:stop(), and their specification clearly says that they are used to avoid non-zero exit code error. But still I get the same error. How can I solve this problem?
EDIT The code as required by a comment:
-module(factorial).
-export([main/0]).
main() ->
{ok, [No_of_cases]} = io:fread("", "~d"),
loop(No_of_cases).
loop(0) ->
%init:stop();
halt(1);
loop(No_of_cases) ->
{ok, [Number]} = io:fread("", "~d"),
ResultFactorial = find_factorial(Number,1),
io:format("~p~n",[ResultFactorial]),
loop(No_of_cases-1).
find_factorial(0,Product) ->
Product;
find_factorial(Number,Product) ->
find_factorial(Number-1,Product*Number).
I got the answer. The trick is that your module name always has to be tested and the entry point should be function main . For example, after compilation it should be run as tested:main().

Resources