Deduct "New List" with "new Value" only for some "specific Tag", from Old List, using "List Comprehension".

Oliver Korpilla Oliver.Korpilla@REDACTED
Sun Jul 19 09:41:11 CEST 2020


Sorry, I didn't see you required list comprehension. Don't know why you have this requirement, though - many other ways to do it.

TV = [{a,1}, {off,1234}, {d,4}, {e,5}, {off,5678}],
Change = 
fun ({off, _}) -> {off, 255};
    (Any)      -> Any
             end,
[ Change(X) || X <- TV ].

Just extract the mapping into a function, don't try to cram it into the right part of the comprehension.

Gesendet: Sonntag, 19. Juli 2020 um 09:31 Uhr
Von: "Oliver Korpilla" <Oliver.Korpilla@REDACTED>
An: "Papa Tana" <papa.tana101@REDACTED>
Cc: "erlang-questions" <erlang-questions@REDACTED>
Betreff: Re: Deduct "New List" with "new Value" only for some "specific Tag", from Old List, using "List Comprehension".
TV = [{a,1}, {off,1234}, {d,4}, {e,5}, {off,5678}],
Change = fun ({off, _}) -> {off, 255};
(Any) -> Any
end,
NewTV = lists:map(Change, TV).
 

Gesendet: Sonntag, 19. Juli 2020 um 08:55 Uhr
Von: "Papa Tana" <papa.tana101@REDACTED>
An: "erlang-questions" <erlang-questions@REDACTED>
Betreff: Deduct "New List" with "new Value" only for some "specific Tag", from Old List, using "List Comprehension".
Hi All,

I have a list "TV" Containing some {Tag, Value} as below:

TV = [{a,1}, {off,1234}, {d,4}, {e,5}, {off,5678}].

I would like to replace all tag name "off" in this list, to a new value = 255.

Wanted result:
[{a,1}, {off,255}, {d,4}, {e,5}, {off,255}].

I can only get the below, I don't know how to change them:
[ {Tag1, Value1} || {Tag1, Value1} <- TV, Tag1/=off].
[ {Tag1, Value1} || {Tag1, Value1} <- TV, Tag1==off].


Any advice please?
Thanks,
Best Regards,


More information about the erlang-questions mailing list