1
votes

I cannot for the life of me get lists:keyfind to work as I expect in Erlang.

I have the following eunit test:

should_find_key_test() ->
    NewList = lists:keystore("key", 1, [], {"key", "value"}),
    Value = case lists:keyfind("key", 1, NewList) of
        false ->
            notfound;
        {_key, _value} ->
            _value
    end,
    ?debugVal(Value).

Whenever I run this test I get the following error message:

indextests:should_find_key_test (module 'indextests')...failed ::error:undef in function lists:keyfind/3 called as keyfind("key",1,[{"key","value"}]) in call from indextests:should_find_key_test/0

Can anyone see what I am doing wrong?

Is it saying that lists:keyfind no longer exists?

1
Which version of Erlang are you running? lists:keyfind/3 is quite recent, though I can't remember in which version it was added. - rvirding

1 Answers

3
votes

lists:keyfind/3 was introduced in OTP/R13A. I suspect you are using an older version.. Prior to R13A you would use lists:serachkey/3. The same tuple is found, but the returned data is structured a little different.

should_find_key_test() ->
    NewList = lists:keystore("key", 1, [], {"key", "value"}),
    Value = case lists:keysearch("key", 1, NewList) of
        false ->
            notfound;
        {value {_Key, _Value}} ->
            _Value
    end,
    ?debugVal(Value).

The release notes show the keyfind/3 BIF being added in STDLIB version 1.6. Check your STDLIB version.