How to get the return value from function in elixir other than :ok?

I have a function, which call another private function in the same module

def index(conn, _params) do


result=date(rounds)
end

defp date(rounds) do


a=#some output
end
I want the output sotred in ‘a’ to be returned to index function…
Is there any way???

This is not clear to me… what you mean by some output?

Is better that you past the full code for both functions, and we will be able to help you without the need to guess what you are trying to do.

1 Like
def index(conn, _params) do
    gl_id=elem(BingbangbobAppWeb.GlobalVariables.gl_id,1) 
    gl_name=:dets.lookup(gl_id, :gl_name)
    gl_name=gl_name[:gl_name]
    ans=Repo.all(from a in Season, where: a.name ==^gl_name, select: %{id: a.id,name: a.name})
    d=hd(ans)
    rounds = Repo.all(from a in Round, join: b in Season,preload: [:season], where: b.id==^d.id and a.season_id==b.id, order_by: a.id )
    result=date(rounds)
    render(conn, "index.html", rounds: result, name: d)
  end
defp date(rounds) do
    Enum.each rounds, fn round ->
      a=round
      tip_cut_off=a.tip_cut_off
      tip_cut_off=tip_cut_off <> "Z"
      {:ok, dt} = Elixir.Timex.Parse.DateTime.Parser.parse(tip_cut_off, "{ISO:Extended}", Timex.Parse.DateTime.Tokenizers.Default)
      tip_cut_off1=Timex.shift(dt, hours: +10, minutes: 0)
      tip_cut_off1=DateTime.to_string(tip_cut_off1)
      tip_cut_off1=String.replace(tip_cut_off1," ", "T")
      tip_cut_off1=hd(String.split(tip_cut_off1,"Z"))
    a=a|>Map.put(:tip_cut_off1,tip_cut_off1)
     end
end

Now i want that ‘a’ to be returned to the index function so that it gets sotred in ‘result’ and i can use it there…

Enum.each returns :ok. If you want something different to be returned you need to use another function. Probably Enum.map or Enum.reduce.

6 Likes
  tip_cut_off1=Timex.shift(dt, hours: +10, minutes: 0)
  tip_cut_off1=DateTime.to_string(tip_cut_off1)
  tip_cut_off1=String.replace(tip_cut_off1," ", "T")
  tip_cut_off1=hd(String.split(tip_cut_off1,"Z"))

This looks like a great opportunity to use |> to make the code more readable. Also I’d suggest using the Elixir code formatter!

3 Likes

Ya…Sure :smile:

Thank you so much… Solved the issue…:smiley:

In elixir result of last executed statement is automatically returned. Here you can do the following

def date(rounds) do
    a = #some output
   a
end
1 Like

Thank you… :smiley: