[erlang-questions] (Newbie) extracting value for one key

Roland Braito r.b.lists@REDACTED
Tue Aug 19 14:52:56 CEST 2008


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@REDACTED>
> An: erlang-questions@REDACTED
> 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@REDACTED
> 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



More information about the erlang-questions mailing list