1

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

3
  • Your question is not clear. What is the sample input? What is the desired output? Commented Dec 10, 2014 at 15:28
  • What the current output is would also be good to know. If I'm reading the code right, I think that the include and related statements aren't working because you're searching only by name and not by name.to_s+","+score.to_s. But it's hard to answer without really knowing what you're trying to get as a result. Commented Dec 10, 2014 at 15:32
  • The program should overwrite the names when it already exists and the score is higher, but it does not work because the name and the score in the array is not splitted. The array looks like this: ["nico,6\n","henrik,9\n","luis,2\n"] The newlines are there because of the array being loaded from the file. Commented Dec 10, 2014 at 15:37

2 Answers 2

1

I think that your program will be more efficient if you move the data to a hash instead of an array.

array.each {|set| set.split(",")}
array=array.to_h

Then you can easily just compare values and replace.

if num > array[name]
  array[name]=num
end

The alternative as far as I am aware would be for you to split the value when you are comparing. I don't actually see any comparison in your program. Something like

@Bestenlite.split(",")
  if @Bestenliste[0]==name
    if @Bestenliste[1].to_i<score

something like that, but that is going to heavily complicate your code.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, to be honest I never used a hash before so I just used a array for like everything. I should read a bit so I can do that. Thanks for the tip!
Once you get them they are super helpful. Good luck. ruby-doc.org/core-2.1.5/Hash.html#method-i-to_h
0

So you want to use a regex to search through your array and replace a specific index if found? Check this post, about using regex to search through an array. I think you should be able to make use Mladen Jablanović's answer there. You can read up on how to use Ruby String's =~ regex matcher here.

I think that you could accomplish it via

x = @Bestenliste.index{|e| e =~ /#{Regexp.quote(name)}.*/ }
unless x.nil?
  @Bestenliste.delete_at(x)
end
@Bestenliste = @Bestenliste +  [name.to_s + "," + score.to_s]

I used this to create and test the regex, and this stackoverflow post for the string interpolation.

7 Comments

Thanks for the answer, after I trief it the first time it just wrote the score in the empty highscore.txt. After trying it the second time it said ln 38: in '*': wrong number of argumentsthats the line: x = @Bestenliste.index{|e| e =~ /#{Regexp.quote(name).*}/ }
Oh, I made a mistake in my regex! I put in the interpolation after I tested it. The .* needs to be on the outside of the }, so it should look like /#{Regexp.quote(name)}.*/. I'll edit my answer to reflect that as well.
Okay, now it says 35: in 'highscore': undefined method 'delete_at' for nil:NilClass Did I broke the array now ?
Well, did you just copy paste my code? Because I think the problem is I made a typo. You can see I accidentally wrote Bentenliste.delete_at instead of Bestenliste.delete_at inside the unless statement. So Ruby's trying to access the array Bentenliste which, as the error code suggests, is nil. Fixing the typo should resolve that.
Error messages can be amazingly helpful if you take the time to read them! They really aren't as difficult to understand as they might first seem.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.