1

my objective is to read a txt.file, and split the contents into an array. However I am having issue with the split command.

My text file content is this

Acosta,1,3,0,0,0
Amezcua,2,1,2,0,2
Avalos,0,1,1,0,0

My code is this

file = open("score2.txt", "r")

file.each do |line|
  array = line.split(",")
  print array
end

However my powershell returns this

["Acosta", "1", "3", "0", "0", "0\n"]["Amezcua", "2", "1", "2", "0", "2\n"]["Avalos", "0", "1", "1", "0", "0\n"]["\n"]

I am confused with what I get from the powershell output. Firstly my txt only contains three lines, why is it that powershell prints out 4 array instead of 3?. Secondly, why is that the element of every last array contains \n, I don't want that. Thirdly if I replaced print with puts, I wouldn't have the issue of \n on every last element of the arrays; why is that so?

To add on, I will explain the reason why I want to remove \n from the last element of the array. The numbers in txt document represents placing of an individual in a tournament whereby e.g. Acosta,1,3,0,0,0 means Acosta scored the first placed in the first match, third in the second match, and no placing in the remaining matches. Hence I want to assign a point system based on the placing each individual scored and finally printing out the total score of each person.

Therefore I used an ifelsif sequence to assign the score as follow

 file = open("score2.txt", "r")

 file.each do |line|
    array = line.split(",")
  name = array.shift
  score = 0
  array.each do |placing|
    if placing == "1"
      score += 6
    elsif placing == "2"
      score += 4
    elsif placing == "3"
      score += 2
    else
      score = score
    end
  end
  print "#{name}: #{score} "
end

However due to \n in the last element of an array, I am unable to assign points to the last match of the tournament. Hence I want to remove the \n from the last element of the arrays.

This exercise is from http://www.evc-cit.info/cit020/beginning-programming/chp_05/exercises2.html.

Can someone kindly enlighten me? Thank you

roppo
  • 57
  • 7

3 Answers3

4

\n represents a new line character.

  • Your text file has a new (blank line) at the very end, so it actually has 4 lines.
  • The last array in your return statement with \n represents the blank new line at the end of your text file.
  • The last element in every array has a \n character because each array represents each of your lines in your text file. Each line is followed by a new line (represented by the \n character).
  • In Ruby, puts adds a new line at the end of each argument, while print uses \n to represent a new line.

If you want to remove the \n from your output, you could simplify your code like this:

open("score2.txt", "r")

file.each do |line|
  print line.split
end

If you want to remove the \n from your output, but preserve new lines, you could use p instead of print:

open("score2.txt", "r")

file.each do |line|
  p line.split
end

If you want your output to look like what you have in your text file, you could use puts:

open("score2.txt", "r")

file.each do |line|
  puts line.split
end

This stackoverflow answer might be helpful, since it discusses the differences between puts and print in Ruby.

This article explains the difference between puts, print, and p in Ruby.

Update:

To remove the \n new lines from your example, add placing = placing.chomp before your conditionals.

You can also use the bang ! operator with chomp to modify the placing string in place i.e. chomp!:

file = open("score2.txt", "r")

file.each do |line|
  array = line.split(",")
  name = array.shift
  score = 0
  array.each do |placing|
    placing.chomp!
    if placing == "1"
      score += 6
    elsif placing == "2"
      score += 4
    elsif placing == "3"
      score += 2
    else
      score = score
    end
  end
  print "#{name}: #{score} "
end
Dan Kreiger
  • 5,358
  • 2
  • 23
  • 27
3

You want line.chomp.split(',').

chomp removes a newline from the end of a string.

Mark Thomas
  • 37,131
  • 11
  • 74
  • 101
1

There are lots of methods available to remove \n like chomp,strip

Try following code:

file = open("score2.txt", "r")

file.each do |line|
  array = line.chomp.split(",")
  array1 = line.strip.split(",")
  print array
  print array1
end
Darshan Patel
  • 3,176
  • 6
  • 26
  • 49