Ruby 构建数据结构-数组哈希的哈希

Ruby 构建数据结构-数组哈希的哈希,ruby,parsing,text,transformation,text-parsing,Ruby,Parsing,Text,Transformation,Text Parsing,本周在工作中,我遇到了解析特定文件格式的挑战 包含按不同站点、区域和类别分类的IP范围 区域。 基本上,我需要一个脚本将所有这些位置信息加载到 数据结构,这将使我能够轻松获得所有IP 用于以后转换的场地、区域或区域 Required data structure: data[Region][Area][Site] -> IPs Hash Hash Hash Array 提前问候并感谢您的建议 Sebastian YEPES我同意Mu的观点,您可以构建一些类,但我认为这应

本周在工作中,我遇到了解析特定文件格式的挑战 包含按不同站点、区域和类别分类的IP范围 区域。 基本上,我需要一个脚本将所有这些位置信息加载到 数据结构,这将使我能够轻松获得所有IP 用于以后转换的场地、区域或区域

Required data structure: data[Region][Area][Site] -> IPs Hash Hash Hash Array 提前问候并感谢您的建议


Sebastian YEPES

我同意Mu的观点,您可以构建一些类,但我认为这应该是可行的:

def processLocations (lines)
  sites     = Hash.new{|h, k| h[k] = []}
  areas     = Hash.new{|h, k| h[k] = {}}
  regions   = Hash.new{|h, k| h[k] = {}}

  lines.each do |line|

  case line
    # Process IPs range section
    when /(.*)=([\d|\-|\.]+)/
      sites[$1.chomp.capitalize] << $2
    # Process area section
    when /(.*)\.area=(.*)/i
      site_key, area = $1.chomp.capitalize, areas[$2.chomp.capitalize]
      area[site_key] = sites[site_key]
    # Process region section
    when /(.*)\.region=(.*)/i
      area_key, region = $1.chomp.capitalize, regions[$2.chomp.capitalize]
      region[area_key] = areas[area_key]
    when /^#.*/ # do nothing
    else
      # error?
    end
  end
  regions
end

Ruby确实有一些类,你知道,你不必把所有的东西都塞进散列和数组中。您可以将大量的逻辑推送到小型定制类中,这样就不会有这么长的processLocations方法应该是&&
# ruby ruby_help.rb
+data---------------------------------------------------------
{"Asia"=>
  {"Australia"=>
    {"Alexandria (alh)"=>["192.168.6.0-192.168.6.127"],
     "Altona"=>["192.168.1.192-192.168.1.255",
"192.168.2.192-192.168.2.255"]},
   "Japan"=>{"Tokyo vpn"=>["192.168.3.192-192.168.3.255"]}},
 "Europe-middle east-africa"=>
  {"France"=>
    {"Paris"=>["192.168.4.192-192.168.4.255"],
     "Rennes"=>["192.168.5.192-192.168.5.255"]}}}
+data['Asia']-------------------------------------------------
{"Australia"=>
  {"Alexandria (alh)"=>["192.168.6.0-192.168.6.127"],
   "Altona"=>["192.168.1.192-192.168.1.255",
"192.168.2.192-192.168.2.255"]},
 "Japan"=>{"Tokyo vpn"=>["192.168.3.192-192.168.3.255"]}}
+data['Asia']['Australia']------------------------------------
{"Alexandria (alh)"=>["192.168.6.0-192.168.6.127"],
 "Altona"=>["192.168.1.192-192.168.1.255",
"192.168.2.192-192.168.2.255"]}
+data['Europe-middle east-africa']['France']['Paris']---------
["192.168.4.192-192.168.4.255"]
def processLocations (lines)
  sites     = Hash.new{|h, k| h[k] = []}
  areas     = Hash.new{|h, k| h[k] = {}}
  regions   = Hash.new{|h, k| h[k] = {}}

  lines.each do |line|

  case line
    # Process IPs range section
    when /(.*)=([\d|\-|\.]+)/
      sites[$1.chomp.capitalize] << $2
    # Process area section
    when /(.*)\.area=(.*)/i
      site_key, area = $1.chomp.capitalize, areas[$2.chomp.capitalize]
      area[site_key] = sites[site_key]
    # Process region section
    when /(.*)\.region=(.*)/i
      area_key, region = $1.chomp.capitalize, regions[$2.chomp.capitalize]
      region[area_key] = areas[area_key]
    when /^#.*/ # do nothing
    else
      # error?
    end
  end
  regions
end