Ruby-访问元素多维数组



谁能帮我改变/组织log条目的逻辑?

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
谢谢你的帮助!

也许你是这个意思:

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]没有问题!

建议的代码有一个错误,如果你实际上定义你的数组如下:

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" 

相关内容

  • 没有找到相关文章