数组中的数字重复

时间:2013-12-04 17:40:12

标签: arrays

我有一个看起来像这样的文件:

15246,025
12486,025
12354,008
41919,003
15246,025
15246,026

是否有可能找到重复的值,然后在最后给出数字给出类似的东西?

12354,008,1
12486,025,1
15246,025,1
15246,025,2
15246,026,3
41919,003,1

这是我到目前为止的代码

class Lotterytickets

attr_accessor :lotnr, :lobnr

def initialize(lotnr,lobnr)

@lotnr=lotnr

@lobnr=lobnr


end

end

myfile = File.open("tickets.txt", "r+")

tickets = Array.new


myfile.each {|line| 
a = line.split(",")

alltic=Lotterytickets.new(a[0],a[1])

tickets << alltic}

tickets.sort! { |x,y| x.lotnr <=> y.lotnr}

tickets.each  do |stu| 

puts "#{stu.lotnr},#{stu.lobnr}"

end

0 个答案:

没有答案