I want to have the code to overwrite existing names when the score is higher than the current in the file.
The file looks like the following: https://i.sstatic.net/LuQwp.png This is the code:
def highscore(name,score)
a = File.new("highscore.txt", "w")
if @Bestenliste.include?("#{name}")
x = @Bestenliste.index("#{name}")
@Bestenliste.delete_at(x)
end
@Bestenliste = @Bestenliste + [name.to_s + "," + score.to_s]
a.puts @Bestenliste
a.close
end
Problem is, that the name and the points in the array are not seperate, and if i want to change that I need to change my whole program. Can I somehow use wildcards or something like that on the name, and also compare the score at the same time ?
I want to overwrite a specific name when it already exists and the score is higher
includeand related statements aren't working because you're searching only bynameand not byname.to_s+","+score.to_s. But it's hard to answer without really knowing what you're trying to get as a result.["nico,6\n","henrik,9\n","luis,2\n"]The newlines are there because of the array being loaded from the file.