tags:

views:

42

answers:

2

Hi, I am not able to print the return output of the following lists:keysearch function.

case lists:keysearch(Query,1,Data) of
    {value,Answer} ->
    io:format("~p",Answer);
    false ->
    io:format("not found ~n")
end,

I know Answer is my tuple that i should print out.Can anyone tell me what mistake i am doing ? thanks in advance

+7  A: 

io:format needs a list of arguments, even if there is only one:

io:format("~p", [Answer]);
legoscia
+1 What's interesting is that he didn't get a badmatch (because Answer is a tuple and not a list). That means keysearch always returns false.
scvalex
@Narayanan Why don't you try running lists:keysearch for some values in the interpreter (erl)? You might discover something interesting.
scvalex
+1  A: 

You probably want to print the key that you found and thus do

{value, {Query, Answer}} -> io:format("~p", [Answer]);
Daniel Luna