nezzart

nezzart

Create a map having a key as a variable

I have a key as a variable, I want to create a Map with a key :key1 or “key2” using one of the variables, but I’m unable to:

iex(24)> a1 = :key1
:key1
iex(25)> a2 = "key2"     
"key2"
iex(26)> %{a1: 123}
%{a1: 123}
iex(27)> %{:a1 => 123}
%{a1: 123}
iex(28)> %{^a2 => 123}
** (CompileError) iex:28: cannot use ^a2 outside of match clauses
    (stdlib) lists.erl:1354: :lists.mapfoldl/3
iex(28)> %{^a1 => 123}
** (CompileError) iex:28: cannot use ^a1 outside of match clauses
    (stdlib) lists.erl:1354: :lists.mapfoldl/3

Marked As Solved

OvermindDL1

OvermindDL1

Don’t use the ^, just put a1 => 123 for it. :slight_smile:

Also Liked

smpallen99

smpallen99

You only use the pin operator ^ inside a match. It is used where you want to match on the value bound to the variable, and not bind a value to the variable.

iex(1)> a = 5
5
iex(2)> a = 7
7
iex(3)> ^a = 7
7
iex(4)> ^a = 5
** (MatchError) no match of right hand side value: 5

Last Post!

smpallen99

smpallen99

You only use the pin operator ^ inside a match. It is used where you want to match on the value bound to the variable, and not bind a value to the variable.

iex(1)> a = 5
5
iex(2)> a = 7
7
iex(3)> ^a = 7
7
iex(4)> ^a = 5
** (MatchError) no match of right hand side value: 5

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement