israelcastilloh

israelcastilloh

How to nest a list into a map?

Hey guys!

I have a single list like this:

[x, y, [1], [2] ... w, z, [3], [4] ... ]

My question is how could i recreate a map which assigns the first 2 as nested maps (or as keys) and the lists within the list as values of the nested maps. Such as it could be expressed as

[ key: x [ key: y, value: [1] [2] ] ]
[ key: w [ key: z value: [3] [4] ] ]

Where 1 and 2 belongs to y which belongs to x.
Where 3 and 4 belongs to z which belongs to w.

Does anyone have an idea how to do this in Elixir?

I am more knowledgeable on python dictionaries. So I was thinking on choosing those elements inside the first list that are “not-list” (x, w) an assign them as keys, and those inside a list (1,2) as values.

Marked As Solved

NobbZ

NobbZ

That’s not JSON, that’s an elixir map.

It doesn’t imply any ordering of keys as well.

For small maps it might sometimes look like the keys are in order of insertion, but this is due to an implementation detail.

It is better to not assume any order in maps.

Also Liked

fuelen

fuelen

defmodule Test do
  def data do
    [
      "JUZGADO PRIMERO CIVIL DE ENSENADA, B.C. 30 DE JULIO DE 2020",
      "PRIMERA SECRETARIA",
      "Acuerdos",
      [
        "1",
        "00268/2016",
        "ROSARIO MEDA MONROY Y OTRAS    VS ANTONIETA GONZALEZ DE CROSTHWAITE TAMBIEN CONOCIDA COMO MARIA ANTONIETA GONZALEZ VIUDA DE CROSTHWAITE ORDINARIO CIVIL PRESCRIPCION POSITIVA"
      ],
      ["2", "00319/2017", "SUCESION INTESTAMENTARIA A BIENES DE, GABRIELA ACEVEDO AMADOR. SUCESORIO INTESTAMENTARIO"],
      [
        "3",
        "00570/2017",
        "SUCESION TESTAMENTARIA A BIENES DE GUADALUPE RIOS ARRIAGA, TAMBIEN CONOCIDA COMO MARIA GUADALUPE RIOS ARRIAGA    VS REGISTRO PUBLICO DE LA PROPIEDAD Y DE COMERCIO Y OTROS ORDINARIO CIVIL PRESCRIPCION POSITIVA"
      ],
      "one",
      "two",
      ["1", "date"],
      ["2", "date"]
    ]
  end

  def process(input, acc \\ %{}) do
    {keys, tail} = Enum.split_while(input, &is_binary/1)
    {values, tail} = Enum.split_while(tail, &is_list/1)
    acc = put_in(acc, Enum.map(keys, &Access.key(&1, %{})), values)

    case tail do
      [] -> acc
      _ -> process(tail, acc)
    end
  end
end

and output

> Test.process(Test.data())
%{
  "JUZGADO PRIMERO CIVIL DE ENSENADA, B.C. 30 DE JULIO DE 2020" => %{
    "PRIMERA SECRETARIA" => %{
      "Acuerdos" => [
        ["1", "00268/2016",
         "ROSARIO MEDA MONROY Y OTRAS    VS ANTONIETA GONZALEZ DE CROSTHWAITE TAMBIEN CONOCIDA COMO MARIA ANTONIETA GONZALEZ VIUDA DE CROSTHWAITE ORDINARIO CIVIL PRESCRIPCION POSITIVA"],
        ["2", "00319/2017",
         "SUCESION INTESTAMENTARIA A BIENES DE, GABRIELA ACEVEDO AMADOR. SUCESORIO INTESTAMENTARIO"],
        ["3", "00570/2017",
         "SUCESION TESTAMENTARIA A BIENES DE GUADALUPE RIOS ARRIAGA, TAMBIEN CONOCIDA COMO MARIA GUADALUPE RIOS ARRIAGA    VS REGISTRO PUBLICO DE LA PROPIEDAD Y DE COMERCIO Y OTROS ORDINARIO CIVIL PRESCRIPCION POSITIVA"]
      ]
    }
  },
  "one" => %{"two" => [["1", "date"], ["2", "date"]]}
}

Is this a structure you are looking for?

fuelen

fuelen

Hello!

Sorry, I do not fully understand your pseudo code.
Could you express desired input and output using elixir terms?

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
stefanchrobot
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
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
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
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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