このスクリプトに何を追加すれば、txt ファイルから重複した行が出力されなくなりますか?
スクリプトは
class TestKeyword
file = File.new("test.txt", "r")
while (line = file.gets)
if line['MAY_DAY']
date = line[/\w+ +\d+ +\d+:\d+:\d+/]
puts "#{date}"
end
end
end
これはテストファイルです:
Oct 15 12:54:01 WHERE IS THE LOVIN MAY_DAY
Oct 16 23:15:44 WHAT THE HECK CAN I DO ABOUT IT HUMP_DAY
Oct 16 14:16:09 I LOVE MY BABY GIRL MAY_DAY
Oct 16 08:25:18 CAN WAIT UNTIL MY BABY RECOVERS CRYSTAL_WIFE
Oct 18 17:48:38 I HOPE HE STOP MESSING WITH THESE FOOLISH CHILDREN TONY_SMITH
Oct 19 05:17:58 GAME TIME GO HEAD AND GET ME MAY_DAY
Oct 20 10:23:33 GAMESTOP IS WHERE ITS AT GAME_DAY
Oct 21 03:54:27 WHAT IS GOING ON WITH MY LUNCH HUNGRY_MAN
Oct 15 12:54:01 WHERE IS THE LOVIN MAY_DAY
Oct 16 23:15:44 WHAT THE HECK CAN I DO ABOUT IT HUMP_DAY
Oct 16 14:16:09 I LOVE MY BABY GIRL MAY_DAY
Oct 16 08:25:18 CAN WAIT UNTIL MY BABY RECOVERS CRYSTAL_WIFE
Oct 18 17:48:38 I HOPE HE STOP MESSING WITH THESE FOOLISH CHILDREN TONY_SMITH
Oct 19 05:17:58 GAME TIME GO HEAD AND GET ME MAY_DAY
Oct 20 10:23:33 GAMESTOP IS WHERE ITS AT GAME_DAY
Oct 21 03:54:27 WHAT IS GOING ON WITH MY LUNCH HUNGRY_MAN
現在、スクリプトを実行すると、次のようになります(これは、キーワード「MAY_DAY」を持つ行の日付と時刻です:
1: Oct 15 12:54:01
1: Oct 16 14:16:09
1: Oct 19 05:17:58
1: Oct 15 12:54:01
1: Oct 16 14:16:09
1: Oct 19 05:17:58
私が必要とする出力は次のとおりです。
1: Oct 15 12:54:01
1: Oct 16 14:16:09
1: Oct 19 05:17:58
重複がないもの