Brian Candler | 19 Aug 14:18
Favicon

(Newbie) extracting value for one key

In Erlang, it seems a list of {key,value} tuples is often returned, e.g.

17> P = process_info(self()).
[{current_function,{erl_eval,do_apply,5}},
 {initial_call,{erlang,apply,2}},
 {status,running},
 {message_queue_len,0},
 {messages,[]},
 {links,[<0.25.0>]},
 {dictionary,[]},
 {trap_exit,false},
 {error_handler,error_handler},
 {priority,normal},
 {group_leader,<0.24.0>},
 {heap_size,610},
 {stack_size,28},
 {reductions,526},
 {garbage_collection,[{fullsweep_after,65535}]}]

Question: What's the idiomatic way to extract the value for a particular
key? For example, to extract "stack_size" from the above I could write

18> hd([ V || {stack_size,V} <- P ]).
28

but is there a better way to do this?

Thanks,

Brian.
(Continue reading)

Gleb Peregud | 19 Aug 14:38
Gravatar

Re: (Newbie) extracting value for one key

On Tue, Aug 19, 2008 at 2:18 PM, Brian Candler <B.Candler <at> pobox.com> wrote:
> In Erlang, it seems a list of {key,value} tuples is often returned, e.g.
>
> 17> P = process_info(self()).
> [{current_function,{erl_eval,do_apply,5}},
>  {initial_call,{erlang,apply,2}},
>  {status,running},
>  {message_queue_len,0},
>  {messages,[]},
>  {links,[<0.25.0>]},
>  {dictionary,[]},
>  {trap_exit,false},
>  {error_handler,error_handler},
>  {priority,normal},
>  {group_leader,<0.24.0>},
>  {heap_size,610},
>  {stack_size,28},
>  {reductions,526},
>  {garbage_collection,[{fullsweep_after,65535}]}]
>
> Question: What's the idiomatic way to extract the value for a particular
> key? For example, to extract "stack_size" from the above I could write
>
> 18> hd([ V || {stack_size,V} <- P ]).
> 28
>
> but is there a better way to do this?
>
> Thanks,
>
(Continue reading)

Brian Candler | 19 Aug 14:55
Favicon

Re: (Newbie) extracting value for one key

> Take a look at module proplists [1], specifically at function
> get_value/2,3 or lookup/2 (or other in there)
> 
> 1: man proplists or http://www.erlang.org/doc/man/proplists.html

Thank you, that was the pointer I needed.

Regards,

Brian.
Torben Hoffmann | 19 Aug 14:45

Re: (Newbie) extracting value for one key

http://www.erlang.org/doc/man/proplists.html

proplists:get_value(stack_size,P).

On Tue, Aug 19, 2008 at 2:18 PM, Brian Candler <B.Candler <at> pobox.com> wrote:
In Erlang, it seems a list of {key,value} tuples is often returned, e.g.

17> P = process_info(self()).
[{current_function,{erl_eval,do_apply,5}},
 {initial_call,{erlang,apply,2}},
 {status,running},
 {message_queue_len,0},
 {messages,[]},
 {links,[<0.25.0>]},
 {dictionary,[]},
 {trap_exit,false},
 {error_handler,error_handler},
 {priority,normal},
 {group_leader,<0.24.0>},
 {heap_size,610},
 {stack_size,28},
 {reductions,526},
 {garbage_collection,[{fullsweep_after,65535}]}]

Question: What's the idiomatic way to extract the value for a particular
key? For example, to extract "stack_size" from the above I could write

18> hd([ V || {stack_size,V} <- P ]).
28

but is there a better way to do this?

Thanks,

Brian.

P.S. I found the example at
http://www.erlang.org/doc/programming_examples/list_comprehensions.html#3.6
which shows how to factor the comprehension into a function.

I also found "element(2, element(2, lists:keysearch(stack_size, 1, P)))."
but that's rather verbose.
_______________________________________________
erlang-questions mailing list
erlang-questions <at> erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions

<div><div dir="ltr">
<a href="http://www.erlang.org/doc/man/proplists.html">http://www.erlang.org/doc/man/proplists.html</a><br><br>proplists:get_value(stack_size,P).<br><br><div class="gmail_quote">On Tue, Aug 19, 2008 at 2:18 PM, Brian Candler <span dir="ltr">&lt;<a href="mailto:B.Candler <at> pobox.com">B.Candler <at> pobox.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote">In Erlang, it seems a list of {key,value} tuples is often returned, e.g.<br><br>
17&gt; P = process_info(self()).<br>
[{current_function,{erl_eval,do_apply,5}},<br>
&nbsp;{initial_call,{erlang,apply,2}},<br>
&nbsp;{status,running},<br>
&nbsp;{message_queue_len,0},<br>
&nbsp;{messages,[]},<br>
&nbsp;{links,[&lt;0.25.0&gt;]},<br>
&nbsp;{dictionary,[]},<br>
&nbsp;{trap_exit,false},<br>
&nbsp;{error_handler,error_handler},<br>
&nbsp;{priority,normal},<br>
&nbsp;{group_leader,&lt;0.24.0&gt;},<br>
&nbsp;{heap_size,610},<br>
&nbsp;{stack_size,28},<br>
&nbsp;{reductions,526},<br>
&nbsp;{garbage_collection,[{fullsweep_after,65535}]}]<br><br>
Question: What's the idiomatic way to extract the value for a particular<br>
key? For example, to extract "stack_size" from the above I could write<br><br>
18&gt; hd([ V || {stack_size,V} &lt;- P ]).<br>
28<br><br>
but is there a better way to do this?<br><br>
Thanks,<br><br>
Brian.<br><br>
P.S. I found the example at<br><a href="http://www.erlang.org/doc/programming_examples/list_comprehensions.html#3.6" target="_blank">http://www.erlang.org/doc/programming_examples/list_comprehensions.html#3.6</a><br>
which shows how to factor the comprehension into a function.<br><br>
I also found "element(2, element(2, lists:keysearch(stack_size, 1, P)))."<br>
but that's rather verbose.<br>
_______________________________________________<br>
erlang-questions mailing list<br><a href="mailto:erlang-questions <at> erlang.org">erlang-questions <at> erlang.org</a><br><a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote>
</div>
<br>
</div></div>
Ahmed Ali | 19 Aug 14:44

Re: (Newbie) extracting value for one key

Hi,

lists:keysearch/3 will do. It returns {value, R}, where R is the result tuple.

> lists:keysearch(stack_size, 1, P).
will return {value, {stack_size, 28}}

BTW, try reading module lists's documentation as it includes a lot of
useful functions.

/ Ahmed Al-Issaei

On Tue, Aug 19, 2008 at 4:18 PM, Brian Candler <B.Candler <at> pobox.com> wrote:
> In Erlang, it seems a list of {key,value} tuples is often returned, e.g.
>
> 17> P = process_info(self()).
> [{current_function,{erl_eval,do_apply,5}},
>  {initial_call,{erlang,apply,2}},
>  {status,running},
>  {message_queue_len,0},
>  {messages,[]},
>  {links,[<0.25.0>]},
>  {dictionary,[]},
>  {trap_exit,false},
>  {error_handler,error_handler},
>  {priority,normal},
>  {group_leader,<0.24.0>},
>  {heap_size,610},
>  {stack_size,28},
>  {reductions,526},
>  {garbage_collection,[{fullsweep_after,65535}]}]
>
> Question: What's the idiomatic way to extract the value for a particular
> key? For example, to extract "stack_size" from the above I could write
>
> 18> hd([ V || {stack_size,V} <- P ]).
> 28
>
> but is there a better way to do this?
>
> Thanks,
>
> Brian.
>
> P.S. I found the example at
> http://www.erlang.org/doc/programming_examples/list_comprehensions.html#3.6
> which shows how to factor the comprehension into a function.
>
> I also found "element(2, element(2, lists:keysearch(stack_size, 1, P)))."
> but that's rather verbose.
> _______________________________________________
> erlang-questions mailing list
> erlang-questions <at> erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
Roland Braito | 19 Aug 14:52

Re: (Newbie) extracting value for one key

Hi!

I'd say

{value,{stack_size, V}} = lists:keysearch(stack_size,1,P),

you can wrap it in some function if that's still too long, i guess...

BR

-------- Original-Nachricht --------
> Datum: Tue, 19 Aug 2008 13:18:32 +0100
> Von: Brian Candler <B.Candler <at> pobox.com>
> An: erlang-questions <at> erlang.org
> Betreff: [erlang-questions] (Newbie) extracting value for one key

> In Erlang, it seems a list of {key,value} tuples is often returned, e.g.
> 
> 17> P = process_info(self()).
> [{current_function,{erl_eval,do_apply,5}},
>  {initial_call,{erlang,apply,2}},
>  {status,running},
>  {message_queue_len,0},
>  {messages,[]},
>  {links,[<0.25.0>]},
>  {dictionary,[]},
>  {trap_exit,false},
>  {error_handler,error_handler},
>  {priority,normal},
>  {group_leader,<0.24.0>},
>  {heap_size,610},
>  {stack_size,28},
>  {reductions,526},
>  {garbage_collection,[{fullsweep_after,65535}]}]
> 
> Question: What's the idiomatic way to extract the value for a particular
> key? For example, to extract "stack_size" from the above I could write
> 
> 18> hd([ V || {stack_size,V} <- P ]).
> 28
> 
> but is there a better way to do this?
> 
> Thanks,
> 
> Brian.
> 
> P.S. I found the example at
> http://www.erlang.org/doc/programming_examples/list_comprehensions.html#3.6
> which shows how to factor the comprehension into a function.
> 
> I also found "element(2, element(2, lists:keysearch(stack_size, 1, P)))."
> but that's rather verbose.
> _______________________________________________
> erlang-questions mailing list
> erlang-questions <at> erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-questions

--

-- 
Psssst! Schon das coole Video vom GMX MultiMessenger gesehen?
Der Eine für Alle: http://www.gmx.net/de/go/messenger03

Gmane