2014-05-14 72 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"