[erlang-questions] Modify a list of tuples

Torben Hoffmann torben.lehoff@REDACTED
Thu Aug 14 23:32:27 CEST 2008


I have not tried this out, but something along the lines of:

modify (OuterList) ->
   lists:map( fun modify_inner/1, OuterList).

modify_inner(List) ->
   lists:map(fun ({b,_}) -> {b,changed};
                            (AllOthers) -> AllOthers
                     end,
                    List).

If each "key" only occurs once in the inner lists you can use

modify_inner(List) ->
    lists:keyreplace(b, 1, List, {b,changed}).

Again, as Justin says, it depends on what characteristics you want the
function to have.
The lists <http://www.erlang.org/doc/man/lists.html> module is always a good
place to look, but other libraries in the stdlib such as dict, sets, gb_sets
might have something that can help you. It depends on the context for your
particular problem.

Cheers,
Torben

On Thu, Aug 14, 2008 at 11: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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080814/e5f2e7cd/attachment.htm>


More information about the erlang-questions mailing list