2010-06-06 16 views
2

「10.1.3.1」 「10.1.3.1」 「10.1.3.1」 「10.1.3.1」 「10.1.3.1」 「10.1.3.1」 「10.1.3.1」 「10.1.3.1」 「10.1.3.1」 「10.1.3.1」 「10.1.3.1」 「10.1.3.1」 零 「10.1.3.4」 「10.1.3.4」 「10.1.3.4」 「10.1.3.4」 「10.1.3.4」 「10.1.3.4」 「10.1.3.4」 「10.1.3.4」 「10.1.3.4」 「10.1.3.4」 「10.1.3.4」 「10.1.3.4」 零 「10.1.3.10」 「10.1.3.10」 「10.1.3.10」 「10.1.3.10」 「10.1.3.10」 「10.1.3.10」 「10.1.3.10」 「10.1.3.10」 「10.1.3.10」 「10.1.3.10」 「10.1.3.10」 「 10.1.3.10「 無 」10.1.3.11「 」10.1.3.11「 」10.1.3.11「 」10.1.3.11「 「10.1.3.11」 「10.1.3.11」 「10.1.3.11」 「10.1.3.11」 「10.1.3.11」 「10.1.3.11」 「10.1.3.11」 零 「10.1.3.12」 「10.1.3.12」 「10.1.3.12」 「10.1.3.12」 「10.1.3.12」 「10.1.3.12」 「10.1.3.12」 「10.1.3.12」 「10.1.3.12」 「 10.1.3.12「 」10.1.3.12「 無 」10.1.3.30「 」10.1.3.30「 無 「10.1.3.38」 「10.1.3.38」 「10.1.3.38」 「10.1.3.38」 「10.1.3.38」 零 「10.1.3.55」 「10.1.3.55」 「10.1.3.55」 「10.1.3.55」 「10.1.3.55」 「10.1.3.55」 「10.1.3.55」 「10.1.3.55」 「10.1.3.55」 「10.1.3.55」 「10.1.3.55」 「 10.1.3.55「 」10.1.3.55「 」10.1.3.55「 」10.1.3.55「 」10.1.3.55「 」10.1.3.55「 「10.1.3.55」 「10.1.3.55」 「10.1.3.55」 「10.1.3.55」 「10.1.3.55」 「10.1.3.55」 「10.1.3.55」 「10.1.3.55」 「 10.1.3.55" 「10.1.3.55」 「10.1.3.55」 「10.1.3.55」 「10.1.3.55」 零 「10.1.3.60」 「10.1.3.60」 「10.1.3.60」 「 10.1.3.60「 」10.1.3.60「 」10.1.3.60「 」10.1.3.60「 無 」10.1.3。66" 「10.1.3.66」 「10.1.3.66」 「10.1.3.66」 「10.1.3.66」 「10.1.3.66」 「10.1.3.66」 零 「10.1.3.101」 「10.1。 3.101" 零 「10.1.3.102」 「10.1.3.102」 「10.1.3.102」 「10.1.3.102」 「10.1.3.102」 「10.1.3.102」 「10.1.3.102」 「10.1。 3.102「 」10.1.3.102「 」10.1.3.102「 」10.1.3.102「 」10.1.3.102「 「10.1.3.102」 「10.1.3.102」 「10.1.3.102」 「10.1.3.102」 「10.1.3.102」 「10.1.3.102」 「10.1.3.102」 「10.1.3.102」 「 10.1.3.102" 「10.1.3.102」 零 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「 10.1.3.103「 」10.1.3.103「 」10.1.3.103「 」10.1.3.103「 」10.1.3.103「 」10.1.3.103「 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「 10.1.3.103" 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「10.1。 3.103「 」10.1.3.103「 」10.1.3.103「 」10.1.3.103「 」10.1.3.103「 」10.1.3.103「 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「10.1.3.103」 「 10.1.3.103" 「10.1.3.103」 零 「10.1.3.104」 「10.1.3.104」 零 「10.1.3.106」 「10.1.3.106」 零 「10.1.3.107」 「10.1。 3.107「 」10.1.3.107「 」10.1.3.107「 」10.1.3.107「 」10.1.3.107「 「10.1.3.107」 零 「10.1.3.108」 「10.1.3.108」 「10.1.3.108」 「10.1.3.108」 「10.1.3.108」 「10.1.3.108」 零 「10.1。 3.110「 」10.1.3.110「 」10.1.3.110「 」10.1.3.110「 」10.1.3。110" 零我怎麼能算在紅寶石下面的字符串IP地址的唯一實例數

上述字符串標準輸出的:

#!/usr/bin/ruby 

require "rubygems" 
require "fastercsv" 


scannedIPs = Hash.new(0) 

count = 0 
FCSV.foreach("HOUND-1.csv", :headers => true, :skip_blanks => false) do |row| 

     text = row[1] 
     p text 

end 

回答

0
#!/usr/bin/ruby 

require "rubygems" 
require "fastercsv" 


scannedIPs = Hash.new(0) 

count = 0 
ips = [] 
FCSV.foreach("HOUND-1.csv", :headers => true, :skip_blanks => false) do |row| 

     text = row[1] 
     ips << text if text # you need the 'if text' for the nil objects. 

end 

num_unique = ips.uniq.length 

現在,num_unique將具有您需要的值。

編輯: 如果您要訪問一個特定的IP頻率計數,這樣做:

#!/usr/bin/ruby 

require "rubygems" 
require "fastercsv" 


scannedIPs = Hash.new(0) 

count = 0 
ips = Hash.new {|h, k| h[k] = 0 } 
FCSV.foreach("HOUND-1.csv", :headers => true, :skip_blanks => false) do |row| 

     text = row[1] 
     ips[text] += 1 if text 

end 

現在「IPS」是IP地址及其頻率的哈希值。 要訪問特定的一個,這樣做:

ips['192.168.0.1'] #=> 0 
ips['10.1.3.110'] #=> a big number 
+0

阿德里安, 太感謝你了,你不知道我是多麼的感激之情,只是多了一個問題: num_unique給我的所有唯一IP的 我怎樣才能得到每個唯一IP的實例數 謝謝, -Kamal。 – kamal 2010-06-07 00:58:44

+0

@kamal:看我的編輯。 – Adrian 2010-06-07 02:15:40

+0

您可以使用更簡單的構造函數'Hash.new(0)' - 無需在這裏使用'Proc'。 – samuil 2010-06-07 20:15:03

2

這個問題(計數獨特元件的問題)的標準方法是使用一個set

require 'set' 

s = Set.new 

s.add('cat') 
s.add('dog') 
s.add('cat') 

p s.size # => 2 (because 2 unique elements) 

如果你不想來算nil作爲一個元素,你必須明確地跳過添加它,或者忽略它在你的尺寸,即

ary.each do |x| 
    s.add(x) unless x.nil? 
end