How to find given atom as a string?

Hi guys! I do have given map:

%RewardappWeb.User{
  __meta__: #Ecto.Schema.Metadata<:loaded, "users">,
  april: 50,
  august: 50,
  december: 50,
  february: 50,
  id: 1,
  january: 50,
  july: 50,
  june: 50,
  march: 50,
  may: 50,
  name: "mat",
  november: 50,
  october: 50,
  points: 0,
  role: "member",
  september: 50,
  surname: "osinski"
}

and I do have a function, which takes a current date and return a STRING as a month

def currentMonth(conn) do
date = Date.utc_today()
month = date.month
IO.inspect(month)

case month do
  1 ->
    month = "january"
  2 ->
    month = "february"
  3 ->
    month = "march"
  4 ->
    month = "april"
  5 ->
    month = "may"
  6 ->
    month = "june"
  7 ->
    month = "july"
  8 ->
    month = "august"
  9 ->
    month = "september"
  10 ->
    month = "october"
  11 ->
    month = "november"
  12 ->
    month = "december"
end

My map is stored here, in the sessionUser:
sessionUser = Plug.Conn.get_session(conn, :userInfo)

I would like to search in my map given month (from currentMonth function). I am making
currentMonth = currentMonth(conn)
and then I would like to take given month based on currentMonth from my map. How could I take that?

@MatOsinski, as @benwilson512 said in your earlier post: “Also, can you take a moment and edit your post to fix the code formatting issues?” You’re asking for help, which is far more likely to be forthcoming if you take care of your formatting. That said:

case month do
  1 ->
    month = "january"
  2 ->
    month = "February"
....

Will not bind the variable month outside the scope of the case statement. You probably meant:

month =
  case month do
    1 -> "January"
    2 -> "February"
    ...
  end

I think then you’d find a map appropriate:

months = %{1 => "January", 2 => "February", ...}
my_month = Map.fetch!(months, some_month)

I don’t really understand the rest of your question but perhaps it will be clearer to understand with some formatting.

1 Like

Sorry about that, I have managed to change the formatting here, but I can not change it in the previous post. I have accomplished my idea in this way

    sessionUser = Plug.Conn.get_session(conn, :userInfo)
    IO.inspect(sessionUser)
    currentMonth = currentMonth(conn)
    IO.inspect(currentMonth)

    currentPoints = Map.get(sessionUser, String.to_atom(currentMonth))

Where sessionUser is mentioned list. However, I am glad for your hints and highly would thank you for that!