erix
Dynamically reference a map key
I have a map:
@legacy %{
wooabsa: "woocommerce_gateway_absa",
woocommerce_absa: "woocommerce_gateway_absa",
woompesa: "woocommerce_gateway_mpesa",
woocommerce_mpesa: "woocommerce_gateway_mpesa",
wookopokopo: "woocommerce_gateway_kopokopo",
woocommerce_kopokopo: "woocommerce_gateway_kopokopo",
givempesa: "mpesa_for_givewp",
}
In PHP, I could reference a map (an object) dynamically:
$legacy = (object) array(
'wooabsa' => 'woocommerce_gateway_absa',
'woocommerce_absa' => 'woocommerce_gateway_absa',
'woompesa' => 'woocommerce_gateway_mpesa',
'woocommerce_mpesa' => 'woocommerce_gateway_mpesa',
'wookopokopo' => 'woocommerce_gateway_kopokopo',
'woocommerce_kopokopo' => 'woocommerce_gateway_kopokopo',
'givempesa' => 'mpesa_for_givewp',
);
$get_dynamically = "wooabsa"
$value = $legacy->{$get_dynamically} # value would be woocommerce_gateway_absa
How can I dynamically set an atom.. or the map key?
I have tried the following (each line is a separate try):
p = data.payload.l # input to the program
IO.puts("#{p} = " <> @legacy[:"woocommerce_absa"]) # works: mpesa_for_givewp = woocommerce_gateway_absa
IO.puts("#{p} = " <> "@legacy[:#{p}]") # works: mpesa_for_givewp = @legacy[:mpesa_for_givewp]
IO.puts("#{p} = " <> Map.get( @legacy, p )) # ArgumentError
IO.puts("#{p} = " <> Map.get( @legacy, ":#{p}" )) # ArgumentError
IO.puts("#{p} = " <> @legacy[:"#{p}"]) # ArgumentError
IO.puts("#{p} = " <> @legacy[":#{p}"]) # ArgumentError
IO.puts("#{p} = " <> @legacy[String.to_atom(p)]) # ArgumentError
# The ArgumentError
** (ArgumentError) construction of binary failed: segment 3 of type 'binary': expected a binary but got: nil
Is there a way to dynamically reference an atom map key?
Marked As Solved
erix
Please disregard.. I’ve just seen my mistake.. p == mpesa_for_givewp which does not exist in the map..
0
Last Post!
dimitarvp
Can you show the actual value of the p variable?
As you have found out, there are a number of perfectly valid ways to get data from a map with a key that’s stored in a variable.
0
Popular in Questions
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
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
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
Other popular topics
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
I would like to know what is the best IDE for elixir development?
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
What learn first? Rust or Elixir
Hi Elixir community!
I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









