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…
Popular in Questions
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Background
Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Other popular topics
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including.
What is Phoenix LiveV...
New







