Pattern match first element of list value in a map

Hi,

Given

test = %{a: 1, b: [%{c: 1,d: 2}] }

How do I pattern match to get values c and d? I tried the following:

%{a: a, [%{c: value1, d: value2} | _ ] = b} = test

but it gives me:

** (SyntaxError) iex:9: syntax error before: ‘[’

Is there a way to pattern match the nested values in the list?

Thanks for the help!

The pattern should be %{a: a, b: [%{c: value1, d: value2} | _ ] = b}, what you wrote is not valid syntax for a map pattern
The = b means the value will be bound to b, not that the key is b. You forgot the key in the pattern.

You have a few syntax errors.

test = %{a: 1, b: [%{c: 1, d: 2}, %{c: 3, d: 4}, %{c: 5, d: 6}, %{c: 7, d: 8}] }
%{a: a, b: [%{c: value1, d: value2} | rest ] } = test

IO.inspect a
IO.inspect value1
IO.inspect value2
IO.inspect rest

1
1
2
[%{c: 3, d: 4}, %{c: 5, d: 6}, %{c: 7, d: 8}]