散列数组-Nil类错误

时间:2018-07-12 07:29:01

标签: ruby-on-rails ruby

我已将输入日志数据重组为以下结构,并尝试导出为ex​​cel电子表格。在将值推入excel工作表时,我面临以下问题,并且我已经检查了数组是否存在任何nil类,但是结果为false,我不确定问题出在哪里。

final = [ {:server=>"new_US_dof1_new"}
{:name=>"OLAUS51", :data=>{"HAL"=>"6.93817139", "CCN_RESP"=>".035400391", "PA_RESP"=>".032287598", "PE_RESP"=>".000610352"}}
{:name=>"OLAUS10", :data=>{"HAL"=>"52.7266846", "CFG_RESP"=>"15.9489746"}}
{:name=>"IdofQA03", :data=>{"PA_RESP"=>".096374512"}}
{:name=>"QEMDB", :data=>{"HAL"=>"22.2698975", "PA_RESP"=>"11.7857666", "CCN_RESP"=>".229125977", "PE_RESP"=>".007202148"}}
{:name=>"OLAUS64", :data=>{"CCN_RESP"=>".757202148"}}
{:name=>"IISCR70", :data=>{}}
{:name=>"IQDV500", :data=>{}}
{:name=>"OLAUS80", :data=>{"PA_RESP"=>"10.5393066", "HAL"=>"4.73620605", "CCN_RESP"=>".023864746", "PE_RESP"=>".003723145", "OPENREPORTS"=>".001708984", "REPORT_RESP"=>".001281738"}}

{:server=>"new_US_dof2_new"}
{:name=>"IBDVHAL", :data=>{"HAL"=>"1.81048584", "FM_RESP"=>".001098633"}}
{:name=>"DEVSITEP", :data=>{"HAL"=>".088012695", "PE_RESP"=>".000610352"}}
{:name=>"OLAUS87", :data=>{"CFG_RESP"=>"1.57061768", "HAL"=>".47253418", "PA_RESP"=>".011962891", "PE_RESP"=>".007202148"}}

same format for other servers]

错误

in `block (2 levels) in <main>': undefined method `each' for nil:NilClass (NoMethodError)
        from excel.rb:34:in `each'
        from excel.rb:34:in `block in <main>'
        from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/axlsx-2.0.1/lib/axlsx/workbook/workbook.rb:262:in `add_worksheet'

我正在尝试使用以下格式的excel表格格式化数据

SERVER               NAME         DATA         GB

new_US_dof1_new"     OLAUS51      HAL          6.93817139
                                  CCN_RESP     .035400391
                                  PA_RESP      .032287598
                                  PE_RESP      000610352

                     OLAUS10      HAL           52.7266846
                                  CFG_RESP      15.9489746       

Likewise for other NAME & SERVER

我想出的代码

require 'csv'
require 'axlsx'

final = []
current = -1

str = File.read("combined.log")
str.each_line do |line|
  line.strip!
  next if line.empty?
  next if line.include?('selected') || line.include?('IN_GB')
  next if ['-','='].include? line[0]
  parts = line.split ' '
   if parts.size == 1 and line.start_with?('size')
    final[current += 1] = {server: line.strip}
    next
  elsif parts.size == 1 and parts = /^(?!.*size_).*$/
    final[current += 1] = {name: line, data: {}}
    next
  end

  parts.each_cons(2) do |key, value|
    final[current][:data][key] = value 
    end
end

#puts final

p = Axlsx::Package.new

wb = p.workbook
wb.add_worksheet(:name => "Basic") do |sheet|
    sheet.add_row ["SERVER", "NAME", "DATA", "GB]"]
    final.each do |val|
      sheet << [ val[:server], val[:name] ]
      val[:data].each do |k, v|
       sheet << ["", "", k, v]
      end
    end
end

p.serialize 'server.xlsx'

发布日志文件的一部分以供参考:

---- db name ----
new_US_dof1_new

OLAUS51


RESP                    IN_GB
------------------------------ ----------
HAL                6.93817139
CCN_RESP               .035400391
PA_RESP            .032287598
PE_RESP            .000610352


=========================================


---- db name ----


OLAUS10


RESP                    IN_GB
------------------------------ ----------
HAL                52.7266846
CFG_RESP               15.9489746
CCN_RESP               .401794434
PA_RESP            .346679688
PE_RESP            .008056641


=========================================
I"m skipping the values for server 1 & proceeding to server 2 values for better readability 

---- db name ----
new_US_dof2_new

IBDVHAL

RESP                    IN_GB
------------------------------ ----------
HAL                1.81048584
FM_RESP           .001098633

=========================================


---- db name ----

DEVSITEP

RESP                    IN_GB
------------------------------ ----------
HAL                088012695
FM_RESP           .000610352

and so on for other server and values

1 个答案:

答案 0 :(得分:1)

in `block (2 levels) in <main>': undefined method `each' for nil:NilClass (NoMethodError)

这意味着在第34行(2级)的块内发生错误。因此,val[:data].each do |k, v|。来自final数组的第一个哈希是{:server=>"new_US_dof1_new"},没有键:data

final.each_with_object([]) do |val, line|
  if line.empty?
    line << val.key?(:server) ? val[:server] : ''
    next if val.key?(:server)
  end
  line << val[:name]

  val[:data].each do |k, v|
    line << k << v
    sheet << line.clone
    line.clear
    line << '' << ''
  end
  line.clear
end