2014-05-14 103 views
-4

你能幫助的人我改變/邏輯組織logRuby-訪問元素多維數組

input_array = [ 
    ["2/6/2014", "13:31:12", "IN", "application1", "user1", "machine1"] 
    ["2/6/2014", "13:31:12", "IN", "application2", "user2", "machine2"] 
    ["2/6/2014", "13:31:52", "IN", "application3", "user3", "machine3"] 
    ["2/6/2014", "13:38:37", "OUT", "application1", "user1", "machine1"] 
    ["2/6/2014", "14:46:37", "OUT", "application2", "user2", "machine2"] 
    ["2/6/2014", "15:56:37", "OUT", "application3", "user3", "machine3"] 
] 

如何這個陣列中訪問單個元素..如2/6/2014application1

當我做input_array[1][4],所需的輸出是...

"application1" # and not 6 ... its giving me the 4 character in line 1 

感謝您的幫助!

+3

'線'是什麼?用這個間距,你的代碼是無效的。 – sawa

+1

即使你使那個特定的語法有效,你仍然會得到一個字符串而不是一個數組。 –

回答

0

也許你的意思是這個:

input_line = [ 
    ["2/6/2014", "13:31:12", "IN", "application1", "user1", "machine1"], 
    ["2/6/2014", "13:31:12", "IN", "application2", "user2", "machine2"], 
    ["2/6/2014", "13:31:52", "IN", "application3", "user3", "machine3"], 
    ["2/6/2014", "13:38:37", "OUT", "application1", "user1", "machine1"], 
    ["2/6/2014", "14:46:37", "OUT", "application2", "user2", "machine2"], 
    ["2/6/2014", "15:56:37", "OUT", "application3", "user3", "machine3"]] 

然後,您可以使用語法input_array[i][j]沒有問題!

+0

是的,你說得對。必須弄清楚如何在每個數組之後得到逗號......感謝隊友 – skumo

0

你提出的代碼中有一個錯誤,如果你真的確定你的陣列是這樣的:

input_array = [ 
    ["2/6/2014", "13:31:12", "IN", "application1", "user1", "machine1"], 
     ["2/6/2014", "13:31:12", "IN", "application2", "user2", "machine2"], 
     ["2/6/2014", "13:31:52", "IN", "application3", "user3", "machine3"], 
     ["2/6/2014", "13:38:37", "OUT", "application1", "user1", "machine1"], 
     ["2/6/2014", "14:46:37", "OUT", "application2", "user2", "machine2"], 
     ["2/6/2014", "15:56:37", "OUT", "application3", "user3", "machine3"] 
    ] 

你會那麼可以訪問陣列,像這樣:

2.0.0p195 :054 > input_array[0][0] 
=> "2/6/2014"