Hello,
A beginner’s question:
I already have code that gives me a list of surnames of unknown length named player_names
. The values are for example ['Oberhagner', 'Windberger', 'Glaser', 'Gschwenner', 'Makatsch']
I also have a struct ‘Player’ with surname:
defmodule Player do
defstruct surname: nil,
forename: nil,
age: nil
end
To put the surname into a Player struct I can do this:
player_xy = Map.put(%Player{}, :surname, Enum.at(player_names, xy))
where xy is the index of my player_names list.
But I want to have a list of those structs. I want to create a list of players that have the surname of the given list.
players = Enum.into(player_names, [], fn x -> Map.put(%Player{}, :surname, Enum.at(player_names, 1)) end)
That works unless that all elements of players now have the surname of the first element of the list of player names (‘Oberhagner’). That’s clear as my code selects the first element (“1”).
But with what should I replace the “1” in my code? Is there an internal counter how often the function x has already be called that I can use?
Or do I have to do that completely different? Thinking in functional programming is still very hard for me.
Thanks for your help.