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 am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
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
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
Hey guys.
I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
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
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
Other popular topics
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
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New







