[erlang-questions] Modify a list of tuples

Justin Sheehy justin@REDACTED
Thu Aug 14 23:15:45 CEST 2008


Dave,

Since you tried a list comprehension, here¹s one that does what you seem to
be asking for:

1>  A = [ [{a,1},{b,2}], [{b,3},{a,4}] ].
[[{a,1},{b,2}],[{b,3},{a,4}]]
2> [[{X,case X of b -> changed; _ -> Y end} || {X,Y} <- Z] || Z <- A].
[[{a,1},{b,changed}],[{b,changed},{a,4}]]

That might not be the best solution depending on the real problem, of
course.

-Justin



On 8/14/08 5:06 PM, "Dave Bryson" <daveb@REDACTED> wrote:

> What's the best way to modify a value for a given tuple in the list
> below?
>
> I have a large list with the following type of structure:
>
> [ [{a,1},{b,2}], [{b,3},{a,4}] ]
>
> I want to preserve the structure of the list and change a value on all
> the tuples with a given key. So for example, change all the values on
> the "b" tuple so the result would be:
>
> [ [{a,1},{b,CHANGED}], [{b,CHANGED},{a,4}] ]
>
> I've tried list comprehension but can't get it to preserve the rest of
> the list.
>
> Thanks in advance!
>
> Dave
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>




More information about the erlang-questions mailing list