Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access complex tuples/maps elixir

How can i access values of aaa, xxx, yyy & zzz from into separate variables, as it does seems mix of maps/tuple.

{{: xxx, %{yyy: 'something', zzz: 'test'}, {aaa: 'best'}}}

Sorry guys for troubling you, i got that working as

iex(1)> {a, b, c} = {: xxx, %{yyy: 'something', zzz: 'test'}, {aaa: 'best'}}
{: xxx, %{yyy: 'something', zzz: 'test'}, {aaa: 'test'}}
iex(2)> a
:xxx
iex(3)> b
%{yyy: 'something', zzz: 'test'}
iex(4)> b[:zzz]
'test'

thanks anyways to all.

like image 271
NewBee Avatar asked May 29 '26 01:05

NewBee


1 Answers

You can even pattern-match on map keys:

iex(1)> {a, %{zzz: b}, %{aaa: c}} = {:xxx, %{yyy: "something", zzz: "test"}, %{aaa: "best"}}
{:xxx, %{yyy: "something", zzz: "test"}, %{aaa: "best"}}
iex(2)> a
:xxx
iex(3)> b
"test"
iex(4)> c
"best"

See http://elixir-lang.org/getting_started/7.html

Btw, in Elixir, single- and doublequotes have different meaning (charlist vs. binary). If you don't really need singlequotes (charlist), use doublequotes everywhere - binary is the default string format in Elixir (see String docs)

like image 190
Miroslav Prymek Avatar answered Jun 02 '26 03:06

Miroslav Prymek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!