How to access a field in Tuple, Map

Hi
Exactly I have this input

{"five_sorted",
 [
   %{
     "down_link" => "https://sample.com/1",
     "mtime" => 1643713487
   },
   %{
     "down_link" => "https://sample.com/2",
     "mtime" => 1643713867
   },
   %{
     "down_link" => "https://sample.com/3",
     "mtime" => 1643713677
   },
   %{
     "down_link" => "https://sample.com/4",
     "mtime" => 1643714056
   }
 ]}
{"status", 1}

I need “down_link”
How can I access this?
For example, {“status”, 1} is an additional
How can I delete this and get to “five_sorted”?

Thanks

1 Like

What have you tried so far?

2 Likes

I tried things like Tuple.to_list and Map.new but I’m not very professional
Unfortunately, I do not know why the api output of one of the sites is like this

1 Like

Pattern matching is commonly used to access tuple data.

{status_of, num} = {"status", 1}

I will leave rest of it for you to figure out as a learning exercise.

2 Likes

break it down by assigning the parts to variables by pattern matching, like

{key_string, list_of_maps} = 
{"five_sorted",
 [
   %{
     "down_link" => "https://sample.com/1",
     "mtime" => 1643713487
   },
   %{
     "down_link" => "https://sample.com/2",
     "mtime" => 1643713867
   },
   %{
     "down_link" => "https://sample.com/3",
     "mtime" => 1643713677
   },
   %{
     "down_link" => "https://sample.com/4",
     "mtime" => 1643714056
   }
 ]}

then just work with list_of_maps

2 Likes

I also tested this

{aa, bb} = input

output:

[
   %{
     "down_link" => "https://sample.com/1",
     "mtime" => 1643713487
   },
   %{
     "down_link" => "https://sample.com/2",
     "mtime" => 1643713867
   },
   %{
     "down_link" => "https://sample.com/3",
     "mtime" => 1643713677
   },
   %{
     "down_link" => "https://sample.com/4",
     "mtime" => 1643714056
   }
 ]
1
1 Like

How can I delete {“status”, 1} as well? Do not show me the output

1 Like

hint is you wont delete anything - you pattern match into variables and access the variable of interest and do further processing.

3 Likes

Usually you don’t delete information from tuples.
Why would you want to delete “status”? to end up with something like {1},
you simply pattern match and do something with that data.

You have these functions if you want to do that,
https://hexdocs.pm/elixir/Tuple.html#summary

But tuples are meant to be read, not to dynamically add/delete information, that is what maps and lists are for.

2 Likes

Think of tuples as a way to return/store information of a fixed shape, for example {:ok, value} or {:error, status, info}

then you pattern match these values and you act accordingly.

def parse() do
   case MyModule.get_info() do
     {:ok, value} ->
        value + 1000

    {:error, status, extra_info} ->
         raise("Failed with status code: #{status}. Additional information: #{inspect(extra_info}")
   end
end
2 Likes

Thank you eksperimental & kartheek

2 Likes

another common function to work with tuples is elem/2, which you use when you care about a particular element in the list, is it commonly used in pipelines when you use the pipe operator |>,
for example.

MyModule.get_info() |> elem(0)

UPDATE: this is used to avoid breaking the pipeline and to avoid pattern matching and storing unnecessary variables, but since you are starting… start with pattern matching until you feel comfortable with it.

2 Likes

Did you go through the official Elixir guide on pattern matching first?

3 Likes

@eksperimental left you with good hints. Expanding them little bit …

Elements in lists can be accessed in multiple ways:

  • Access first using pattern match
[first | rest] = [1, 2, 3]

first will be 1
rest will be [2, 3]
  • access each element in list (iterate a list) - You can use for or Enum module

Then each element is map - you can access value of map using map[key] like

person = %{"id" => 1, "name" => "john"}
person["id"] 

@edxofp If you want answer we can provide directly - share your solution based on hints and people will refine it further.

1 Like

I solved my problem with this method
Thanks to all the friends

defmodule R do
  def test(input) do
    case HTTPoison.post("https://sample.com/", Jason.encode!(%{"user" => input})) do
      {:ok, %{body: body, status_code: 200}} ->
        Jason.decode!(body)["five_sorted"]

      {:error, _} ->
        IO.inspect("_")
    end
  end
end

for dd <- R.test(1) do
  IO.puts(dd["down_link"])
end
1 Like

then Jason.decode!(body) does not return a tuple, but a map.

2 Likes