Ruby访问元素多维数组
您能帮我更改/组织Ruby访问元素多维数组,ruby,arrays,multidimensional-array,Ruby,Arrays,Multidimensional Array,您能帮我更改/组织日志条目的逻辑吗 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/
日志条目的逻辑吗
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/2014
或应用程序1
当我输入数组[1][4]
时,所需的输出是
"application1" # and not 6 ... its giving me the 4 character in line 1
谢谢你的帮助 如果您实际像这样定义数组,则建议的代码有错误:
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"
然后,您可以像这样访问阵列:
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"
也许你的意思是:
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\u array[i][j]
而不会出现问题 什么是行
?用这个间距,你的代码是无效的。即使你使这个特定的语法有效,你仍然会得到一个字符串而不是数组。是的,你是对的。必须弄清楚如何在每个数组后获取逗号。。。谢谢,伙计