(MatchError) no match of right hand side value:

iex(1)> pairs
[
  %{asset: "474jTeYx2r2Va35794tCScAXWJG9hU2HcgxzMowaZUnu", waves_rate: 187.51},
  %{asset: "474jTeYx2r2Va35794tCScAXWJG9hU2HcgxzMowaZUnu", balance: 0.05218729}
]
iex(2)> [%{key: value}] = pairs
** (MatchError) no match of right hand side value: [%{asset: "474jTeYx2r2Va35794tCScAXWJG9hU2HcgxzMowaZUnu", waves_rate: 187.51}, %{asset: "474jTeYx2r2Va35794tCScAXWJG9hU2HcgxzMowaZUnu", balance: 0.05218729}]

What is the proper way to pattern match against pairs here?

This cannot work, there are no :key in pairs, and pairs contains more than one item… You need to know the shape of your data to pattern match correctly.

1 Like

So there’s no way to pattern match against pairs?

Pattern match what? against what? This is not clear what You want.

1 Like

The objective is to extract the waves_rate and balance values from pairs. If not via pattern matching, what’s the best way to accomplish this?

Elements of pairs don’t have the same shape… and it’s a list. Sorry, but You missed something on the way to pattern match.

Maybe You want to pattern match elements of pairs?

You can use this:

[%{waves_rate: waves_rate}, %{balance: balance}] = pairs
4 Likes

Awesome! Thanks so much for your gracious help. I knew there was a way to use pattern matching here :slight_smile: :slight_smile: :slight_smile:

1 Like

You can pattern match a lot of data as @kokolegorille said. But your initial coding attempt made zero sense: there is no key or value map keys in any of the elements in the list you showed. Why did you even try that? How would that work?

I feel by your recent posts that you have misconceptions about various Elixir workings. Have you went through the full intro course on the official website?

That sounds a bit harsh. This might be very obvious to many people here but I also remember being a bit confused over pattern matching at first. Especially when I had to jump from the intro examples to my real world data.

3 Likes

I usually avoid being controversial and this time kind of did it on purpose. The coding sample in the OP seemed very random and smelled like “please do my homework”.

I also based my reply on the author’s history. To me it seems he didn’t cover Elixir’s official tutorial which should be the respectful thing to do in regard to everybody else’s willingness to help.

So yes, I realise what I said was a bit controversial and I am simply explaining my reasons to do so. I am not looking to argue about it however, and won’t make further comments regarding OP, including in their future posts.

1 Like