Updating nested records in a single-line expression?

Rachwal Waldemar-AWR001 Waldemar.Rachwal@REDACTED
Fri Oct 8 09:51:28 CEST 2004


I meant one-line reassign without any intermediate read.
Slight modification of reassign statement of your example and it works fine
as well.
Ance again thanks for your help.
Best regards,
Waldemar.

%%%%%%%%%%%%%%%%%%%%

-module(recs).
-record(arec,{a,b}).
-record(brec,{c,d}).
-export([test/0]).

test() ->
    %%assignment statement
    Arec = #arec{a=hello,b=#brec{c=world,d=again}},

    %%reassign
    Arec2 = Arec#arec{a="hello",b=(Arec#arec.b)#brec{c="world",d="again"}},

    io:format("~p\n",[{Arec,Arec2}]).

-----Original Message-----
From: Peter Lund [mailto:Peter.Lund@REDACTED] 
Sent: Friday, October 08, 2004 8:54 AM
To: Rachwal Waldemar-AWR001; erlang-questions@REDACTED
Subject: SV: Updating nested records in a single-line expression?


do you mean:

-module(recs).
-record(arec,{a,b}).
-record(brec,{c,d}).
-export([test/0]).
%%and then...
test() ->
    %%assignment statement
    Arec = #arec{a=hello,b=#brec{c=world,d=again}},

    %% read
    #arec{a=A,b=Brec=#brec{c=C,d=D}} = Arec,

    %%reassign
    Arec2 = Arec#arec{a="hello",b=Brec#brec{c="world",d="again"}},

    io:format("~p\n",[{Arec,Arec2}]).

Tested and works!

best regards,
Peter Lund

> -----Ursprungligt meddelande-----
> Från: owner-erlang-questions@REDACTED 
> [mailto:owner-erlang-questions@REDACTED]För Rachwal Waldemar-AWR001
> Skickat: den 7 oktober 2004 21:23
> Till: erlang-questions@REDACTED
> Ämne: Updating nested records in a single-line expression?
>
>
> Hi!
>
> Accessing data from nested records is fairly clear.
> However, I experienced notational problems trying to update such 
> records in a single line expression.
> Suppose we have a protocol where messages consist of elements
> which in turn
> are collections of fields being simple values or form further nested
> structures.
> When I modify any nested field I have to get a modified copy
> of the message,
> but how to get it in a single expression without additional variables?
> Is it doable?
> Or, maybe I have to deal with such messages in different way
> rather than
> model as records and then access/update these records directly?
>
> Thanks for any help,
> Waldemar.



More information about the erlang-questions mailing list