erix

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

erix

Please disregard… I’ve just seen my mistake… p == mpesa_for_givewp which does not exist in the map…

Where Next?

Popular in Questions Top

JorisKok
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
openscript
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
aalberti333
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
rms.mrcs
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
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
johnnyicon
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
Fl4m3Ph03n1x
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
siddhant3030
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
joeerl
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 Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
Brian
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
nsuchy
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
script
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
vac
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
qwerescape
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
9mm
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
AstonJ
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

We're in Beta

About us Mission Statement