Syntax for accessing a single value of a map

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Syntax for accessing a single value of a map

Donald Steven
The book "Learn You Some..." notes that the syntax M#{K} may not work
and indeed it results in an error when I tried to use it.  What is the
correct syntax?
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Syntax for accessing a single value of a map

Bryan Paxton
 Hi Donald,

This part of the syntax has not been implemented yet. Currently you need
to use maps:get/2 or maps:get/3 (http://erlang.org/doc/man/maps.html#get-2)


Example:

Eshell V10.2.1  (abort with ^G)
1> M = #{foo => bar}.
#{foo => bar}
2> maps:get(foo, M).
bar
3> maps:get(bar, M, undefined).
undefined
4>


Cheers

--

Bryan


On 1/26/19 2:04 PM, Donald Steven wrote:
> The book "Learn You Some..." notes that the syntax M#{K} may not work
> and indeed it results in an error when I tried to use it.  What is the
> correct syntax?
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Syntax for accessing a single value of a map

Donald Steven
Thanks Brian, that's very helpful.

Best,

Don

On 1/26/2019 4.20 PM, Bryan Paxton wrote:

>   Hi Donald,
>
> This part of the syntax has not been implemented yet. Currently you need
> to use maps:get/2 or maps:get/3 (http://erlang.org/doc/man/maps.html#get-2)
>
>
> Example:
>
> Eshell V10.2.1  (abort with ^G)
> 1> M = #{foo => bar}.
> #{foo => bar}
> 2> maps:get(foo, M).
> bar
> 3> maps:get(bar, M, undefined).
> undefined
> 4>
>
>
> Cheers
>
> --
>
> Bryan
>
>
> On 1/26/19 2:04 PM, Donald Steven wrote:
>> The book "Learn You Some..." notes that the syntax M#{K} may not work
>> and indeed it results in an error when I tried to use it.  What is the
>> correct syntax?
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Syntax for accessing a single value of a map

empro2
What about matching?

M = #{1 => 2}.
#{1 => 2}
143> #{1 := V} = M.
#{1 => 2}
144> V.
2

What about reading the manual? :-)

<http://erlang.org/doc/reference_manual/expressions.html#map_expressions>

Similarly:

%% Instead of Val = Record#rec_name.some_field.
#rec_name{some_field = Val} = Record.

Lest Erlang turns into Java ;-)

Michael

--

You do not live in your environment,
you are part of it.











_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions