Ruby文件处理、读取然后作为数组存储
我无法理解这段代码是如何工作的,程序能够读取文件,但最终会多次打印同一行 输出:Ruby文件处理、读取然后作为数组存储,ruby,Ruby,我无法理解这段代码是如何工作的,程序能够读取文件,但最终会多次打印同一行 输出: Track title is: c/music/1 Track file location is: c/music/1 Track title is: c/music/2 Track file location is: c/music/2 Track title is: c/music/3 Track file location is: c/music/3 Track title is: Taco
Track title is: c/music/1
Track file location is: c/music/1
Track title is: c/music/2
Track file location is: c/music/2
Track title is: c/music/3
Track file location is: c/music/3
Track title is: Taco
Track file location is: c/music/1
Track title is: Burrito
Track file location is: c/music/2
Track title is: Nacho
Track file location is: c/music/3
class Track
attr_accessor :title, :file_location
end
def read_tracks music_file
count = music_file.gets().to_i
tracks = Array.new
$i = 0
while $i < count do
track = read_track(music_file)
tracks << track
$i += 1
end
tracks
end
def read_track aFile
track_title = aFile.gets
track_file_location = aFile.gets
track = Track.new
track.title = track_title
track.file_location = track_file_location
end
def print_tracks tracks
$i = 0
while $i < tracks.length do
print_track(tracks)
$i += 1
end
end
def print_track tracks
puts('Track title is: ' + tracks[$i].to_s)
puts('Track file location is: ' + tracks[$i].to_s)
end
def main
aFile = File.new("input.txt", "r")
if aFile
tracks = read_tracks(aFile)
aFile.close
else
puts "Unable to open file to read!"
end
print_tracks(tracks)
end
main
5
Taco
c/music/1
Burrito
c/music/2
Nacho
c/music/3
预期输出:
Track title is: c/music/1
Track file location is: c/music/1
Track title is: c/music/2
Track file location is: c/music/2
Track title is: c/music/3
Track file location is: c/music/3
Track title is: Taco
Track file location is: c/music/1
Track title is: Burrito
Track file location is: c/music/2
Track title is: Nacho
Track file location is: c/music/3
class Track
attr_accessor :title, :file_location
end
def read_tracks music_file
count = music_file.gets().to_i
tracks = Array.new
$i = 0
while $i < count do
track = read_track(music_file)
tracks << track
$i += 1
end
tracks
end
def read_track aFile
track_title = aFile.gets
track_file_location = aFile.gets
track = Track.new
track.title = track_title
track.file_location = track_file_location
end
def print_tracks tracks
$i = 0
while $i < tracks.length do
print_track(tracks)
$i += 1
end
end
def print_track tracks
puts('Track title is: ' + tracks[$i].to_s)
puts('Track file location is: ' + tracks[$i].to_s)
end
def main
aFile = File.new("input.txt", "r")
if aFile
tracks = read_tracks(aFile)
aFile.close
else
puts "Unable to open file to read!"
end
print_tracks(tracks)
end
main
5
Taco
c/music/1
Burrito
c/music/2
Nacho
c/music/3
代码:
Track title is: c/music/1
Track file location is: c/music/1
Track title is: c/music/2
Track file location is: c/music/2
Track title is: c/music/3
Track file location is: c/music/3
Track title is: Taco
Track file location is: c/music/1
Track title is: Burrito
Track file location is: c/music/2
Track title is: Nacho
Track file location is: c/music/3
class Track
attr_accessor :title, :file_location
end
def read_tracks music_file
count = music_file.gets().to_i
tracks = Array.new
$i = 0
while $i < count do
track = read_track(music_file)
tracks << track
$i += 1
end
tracks
end
def read_track aFile
track_title = aFile.gets
track_file_location = aFile.gets
track = Track.new
track.title = track_title
track.file_location = track_file_location
end
def print_tracks tracks
$i = 0
while $i < tracks.length do
print_track(tracks)
$i += 1
end
end
def print_track tracks
puts('Track title is: ' + tracks[$i].to_s)
puts('Track file location is: ' + tracks[$i].to_s)
end
def main
aFile = File.new("input.txt", "r")
if aFile
tracks = read_tracks(aFile)
aFile.close
else
puts "Unable to open file to read!"
end
print_tracks(tracks)
end
main
5
Taco
c/music/1
Burrito
c/music/2
Nacho
c/music/3
问题在于方法打印轨道和打印轨道
这些方法应该如下所示:
def print_tracks tracks
$i = 0
while $i < tracks.length do
print_track(tracks[$i])
$i += 1
end
end
def print_track track
puts('Track title is: ' + track.title.to_s)
puts('Track file location is: ' + track.file_location.to_s)
end
def print_tracks(tracks)
tracks.each do |track|
puts "Track title is: #{track.title}"
puts "Track file location is: #{track.file_location}"
puts
end
end
在这种情况下,整个代码将为:
class Track
attr_accessor :title, :file_location
end
def read_tracks music_file
count = music_file.gets().to_i
tracks = Array.new
i = 0
while i < count do
track = read_track(music_file)
tracks << track
i += 1
end
tracks
end
def read_track aFile
track = Track.new
track.title = aFile.gets
track.file_location = aFile.gets
track
end
def print_tracks(tracks)
tracks.each do |track|
puts "Track title is: #{track.title}"
puts "Track file location is: #{track.file_location}"
puts
end
end
def main
aFile = File.new("input.txt", "r").
if aFile..
tracks = read_tracks(aFile)
aFile.close
else
puts "Unable to open file to read!"
end
print_tracks(tracks)
end
main
我得到了输出:
Track title is: Taco
Track file location is: c/music/1
Track title is: Burrito
Track file location is: c/music/2
Track title is: Nacho
Track file location is: c/music/3
这正是你所期望的 尝试此操作,将线覆盖到阵列,然后对其进行操作:
lines = File.readlines('tracks.txt') # reads lines into array
lines.reject! { |e| e == "\n" } # removes empti lines
total_tracks = lines.shift.chomp.to_i # extract the first line from the array
lines.each_slice(2) { |e| puts e } # lines now contains only the pair track/directory
# adapt at your will
对于
每个\u片段(2)
请参见,它将数组的元素分组。在输入文本文件中,我们是否可以假设每个曲目信息位于新行上?你能发布输入文件的示例吗?另外,您可以发布一个格式化的输出示例吗?按照您的方式,您似乎希望输出一行文本,而不输出新的文本行。您好,很抱歉这篇文章的格式设置不好。输入文件有7行,第一行包含一个数字,表示“轨迹”的数量。我还需要一个新的行输出。5在输入文件的第一行表示什么?这种模式重复吗?很难理解这个模式是什么。请不要在Ruby中使用全局变量(或者其他任何地方),永远不要。Jörg W Mittag,当我发布解决方案时,我尝试对作者的代码进行最小的更改,以便更容易理解代码的差异。当然,这里不需要全局变量。Jörg W Mittag,我添加了稍微改进的代码版本,只是为了展示它应该是怎样的。嗨,我试过了,但仍然没有得到想要的输出。我认为错误在于读取文件,尤其是将读取的数据存储为数组时。@Pickles,使用新修订的方法print_tracks,程序的输出是什么?