Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to get value from keyword list which key is integer?

The list is like [{0,10},{2,20}]. I have get value 10 when provided by key as 0.

I haven’t find the suitable function in Enum, List, Keyword module, and have to use erlang module proplists to solve the problem. Is it necessary?

inventory_position_list = [{0,10},{2,20}]
x = :proplists.get_value(0,inventory_position_list)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I believe List.keyfind/4 or List.keyfind!/3 are what you are looking for:

iex> List.keyfind!([{0,10},{2,20}], 2, 0)
{2, 20}

This returns the whole matching tuple though, so you’d need to pattern-match on the return:

{_, value} = List.keyfind!(list, key, 0)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading