开发者

loop, array and file problem in ruby

I'm currently learning ruby and here what I'm trying to do: A script which open a file, make a subsitution, then comparing every lines to each other to see if it exist many times. So, I tried to work directly with the string, but I didn't find how to do it, so I put every line in an array, and comparing every row. But I got a first problem. Here is my code:

#!/usr/bin/env ruby

DOC = "test.txt"
FIND = /,,^M/
SEP = "\n"

#make substitution
puts File.read(DOC).gsub(FIND, SEP)

#open the file and put every line in an array
openFile = File.open(DOC, "r+")
fileArray = openFile.each { |line| line.split(SEP) }
#print fileArray #--> give the name of the object
#Cross the array to compare every items to every others
fileArray.each do |items|
items.chomp
        fileArray.each do |items2|
        items2.chomp
                #Delete if the item already exist
                if items = items2
      开发者_运维技巧                  fileArray.delete(items2)
                end
        end
end
#Save the result in a new file
File.open("test2.txt", "w") do |f|
        f.puts fileArray
end

At the end, I only have the name of the array object "fileArray". I print the object after the split, and i've got the same, so I guess the problem is from here. Little help required (if you know how to do this without array, just with the line in the file, answer appreciate too). Thanks !

EDIT: So, here's my code now

#!/usr/bin/env ruby

DOC = "test.txt"
FIND = /,,^M/
SEP = "\n"

#make substitution
File.read(DOC).gsub(FIND, SEP)

unique_lines = File.readlines(DOC).uniq
#Save the result in a new file
File.open('test2.txt', 'w') { |f| f.puts(unique_lines) }

Can't figure out how to chomp this.


Deleting duplicate lines in a file:

no_duplicate_lines = File.readlines("filename").uniq

No need to write so much code :)


Modify your code like this:

f.puts fileArray.join("\n")

Alternate way:

unique_lines = File.readlines("filename").uniq
# puts(unique_lines.join("\n")) # Uncomment this line and see if the variable holds the result you want...
File.open('filename', 'w') {|f| f.puts(unique_lines.join("\n"))}


Just a couple of points about the original code:

fileArray = openFile.each { |line| line.split(SEP) }

sets fileArray to a File object, which I suspect wasn't your intention. File#each (the # notation is Ruby convention to describe a particular method on an object of the supplied class) executes your supplied block for each line (it's also available with a synonym: each_line), where a line is defined by default as your OS's end-line character(s).

If you were looking to build an array of lines, then you could just have written

fileArray = openFile.readlines

and if you wanted those lines to be chomped (often a good idea) then that could be achieved by something like

fileArray = openFile.readlines.collect { |line| line.chomp }

or even (since File mixes in Enumerable)

fileArray = openFile.collect { |line| line.chomp }

And one other tiny thing: Ruby tests for equality with ==, = is only for assignment, so

if items = items2

will set items to items2 (and will always evaluate as true)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜