“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” 零
上面的字符串是stdout:
#!/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 :(得分: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
答案 1 :(得分: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