2017-01-30 155 views
0

我試圖找出爲什麼我不斷收到以下錯誤: enter image description here紅寶石未定義的方法`[]」的零:NilClass(NoMethodError)錯誤

從下面的代碼:

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語句是無效的。我想它會是。任何幫助表示讚賞。謝謝!

+1

能否請您點的行號哪一個是線:27 –

+0

它在你的'file_contents的一個破[I] [5]'好像'file_contents [I]'是零某處 –

+0

大家好,感謝所有的幫助!你們都幫我弄明白了!我很感激。 – John123

回答

1

好像的file_contents一個或file_contents2是空的。

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

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

其中一個陣列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]

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

數組索引從0到長度− 1在Ruby中;代之以在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 
相關問題