簡體   English   中英

Ruby未定義方法`[]'表示nil:NilClass(NoMethodError)錯誤

[英]Ruby undefined method `[]' for nil:NilClass (NoMethodError) error

我想弄清楚為什么我一直收到以下錯誤: 在此輸入圖像描述

從以下代碼:

def information_transfer()
  file_contents = CSV.read("test.csv", col_sep: ",", encoding: "ISO8859-1")
  file_contents2 = CSV.read("applicantinfo.csv", col_sep: ",", encoding:"ISO8859-1")
  arraysize = file_contents.length
  arraysize1 = file_contents2.length
  for i in 1..arraysize
    for x in 1..arraysize1
      if file_contents[i][0] == file_contents2[x][0]
        CSV.open("language_output.csv", "wb") do |csv|
          csv << [file_contents[i][0], file_contents[i][1], file_contents[i][2],file_contents[i][3], file_contents[i][4], 
          file_contents[i][5], file_contents[i][6], file_contents[i][7], file_contents[i][8],file_contents[i][9], 
          file_contents[i][10], file_contents[i][11], file_contents[i][12], file_contents[i][13], file_contents[i][14],
          file_contents[i][15], file_contents[i][16], file_contents[i][17], file_contents[i][18], file_contents2[i][24],file_contents2[i][25], 
          file_contents2[i][26],file_contents2[i][27], file_contents2[i][28], file_contents2[i][29], file_contents2[i][30], file_contents2[i][31], file_contents2[i][32], file_contents2[i][33]]
        end
     end
   end
 end
end

我基本上試圖采用兩個單獨的.csv文件並將某些列合並在一起。 我有兩個數組(file_contents和file_contents2)正在讀取各個csv文件並將內容存儲在數組中。 由於某些原因,我的if語句出現語法錯誤。 我希望有人可以幫我找出為什么我寫的if語句無效。 我想是的。 任何幫助表示贊賞。 謝謝!

您的一個數組file_contentsfile_contents2可能為空。 if語句之前輸出兩者,以及打印file_contents[i][0]file_contents2[x][0]

你可以做一個應該工作的簡單改變:

for i in 0..arraysize for x in 0..arraysize1

並添加錯誤檢查:

if !file_contents[i].blank? and !file_contents2[x].blank? and file_contents[i][0] == file_contents2[x][0]

for i in 1..arraysize
  for x in 1..arraysize1

數組索引在Ruby中從0運行到length - 1; 循環在0...arraysize而不是。

如果 file_contents2[i]可以或應該寫為file_contents2[x] ,則可以直接遍歷數組的內容:

for a in file_contents
  for b in file_contents2

並使用切片將連續的數組元素放入另一個數組:

def information_transfer()
  file_contents = CSV.read("test.csv", col_sep: ",", encoding: "ISO8859-1")
  file_contents2 = CSV.read("applicantinfo.csv", col_sep: ",", encoding: "ISO8859-1")
  for a in file_contents
    for b in file_contents2
      if a[0] == b[0]
        CSV.open("language_output.csv", "wb") do |csv|
          csv << a[0..18] + b[24..33]
        end
      end
    end
  end
end

如果您嘗試一對一地加入這兩個文件,則可以通過將密鑰放入哈希來更有效地實現。 您也可能並不是每次都要重新打開輸出文件。

def information_transfer()
  file_contents = CSV.read("test.csv", col_sep: ",", encoding: "ISO8859-1")
  file_contents2 = CSV.read("applicantinfo.csv", col_sep: ",", encoding: "ISO8859-1")

  h = Hash[file_contents.collect { |row| [row[0], row] }]

  CSV.open("language_output.csv", "wb") do |csv|
    for b in file_contents2
      a = h[b[0]]
      csv << a[0..18] + b[24..33]
    end
  end
end

看起來像file_contentsfile_contents2是空的。

如果您不想在該特定行上引發錯誤,則可以跳過循環。

next if file_contents[i].blank? || file_contents2[i].blank?
if file_contents[i][0] == file_contents2[x][0]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM