<div dir="ltr">I would do neither of those, there's a BIF just for this purpose:<div><br></div><div>lists:keyfind("name3", 1, A)</div><div><br></div><div>One could also use the slower, but perhaps easier to understand proplists (lists:keyfind/3 is more general):</div>
<div><br></div><div>proplists:get_value("name3", A)</div><div><br></div><div>If you are not reading the keys for those pairs from the outside world (a file, the network, etc), I'd use atoms instead of strings (just remove the quotes).</div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Feb 25, 2014 at 8:08 AM, Oleg <span dir="ltr"><<a href="mailto:lego12239@yandex.ru" target="_blank">lego12239@yandex.ru</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hello.<br>
<br>
I'm erlang newbie. Don't beat me, please :-), if this question is obvious.<br>
<br>
I have a list of key-value pairs:<br>
<br>
A=[{"name1", 1}, {"name2", 77}, {"name3", 33}, {"name4", 234}].<br>
<br>
What is faster:<br>
<br>
[ Value || {Name, Value} <- A, string:equal(Name, "name3")].<br>
<br>
Or:<br>
<br>
get_value(Key, []) -><br>
[].<br>
get_value(Key, [H|T]) -><br>
{Name, Value} = H,<br>
case string:equal(Name, Key) of<br>
true -><br>
[Value];<br>
false -><br>
get_value(Key, T)<br>
end.<br>
<br>
start() -><br>
get_value("name3", A).<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Sean Cribbs <<a href="mailto:sean@basho.com" target="_blank">sean@basho.com</a>><div>Software Engineer</div><div>Basho Technologies, Inc.</div><div><a href="http://basho.com/" target="_blank">http://basho.com/</a></div>
</div>