#Find coordinates of every occurrence of element in matrix

17 messages · Page 1 of 1 (latest)

fallen void
#

Hello, I'm trying to find the coordinates of every '' in my nested List, i had the idea of using Enum.with_index to get the indexes of rows and cols, but i fail to continue with searching after the first '', i'm not sure how should i approach it.

median quartz
#

i think you're looking for _ but its not clear from your question

restive valley
#

you meant looking for * ?

fallen void
#

I looking for the * characters and their coordinates, (looking for the stars ), i have managed to get the point when i get the Col numbers for each of them, now i!m trying to add the row numbers to it

restive valley
#

So you have a list of list

#

Right?

median quartz
#

can you paste the data before you ran with index

fallen void
#

the numbers doesnt matter, i remove them

median quartz
#

have you got livebook? this is a good problem for it

fallen void
#

no, i sadly can't use more than the basic Elixir provides

restive valley
#

It isn't a lib

#

It's more like a playground

median quartz
#
data = """
-  *  -  -  -
-  -  -  -  -
-  -  *  -  *
-  -  -  -  -
*  -  -  -  *
"""
``` ```elixir
rows =
  data
  |> String.split("\n")       # split by row
  |> Enum.reverse()           # reverse so coords start at the bottom
  |> Enum.reject(&(&1 == "")) # remove the extra row
``` ```elixir
locations =
  for {row, row_index} <- Enum.with_index(rows) do
    cells =
      row
      |> String.split()    # split by whitespace
      |> Enum.reverse()    # reverse so coords start at the left

    for {"*", col_index} <- Enum.with_index(cells) do
      {row_index + 1, col_index + 1}
    end
  end
``` ```elixir
IO.puts(data)

locations =
  locations
  |> List.flatten()   # remove emptys
#

theres a problem with it but i dont have time to do more, however that should get you close 🙂