二维阵列不应该被覆盖

时间:2017-09-20 16:22:22

标签: arrays ruby multidimensional-array

我正在编写一些从文件中读取输入的代码。

我的代码解析输入文件的内容并将数据存储为2D数组,例如

输入是(请参阅下面的输入文件以获取正确的格式,我无法在此处使格式化):

ABC

DEF

解析2D数组后应该看起来像这样...... [[ 'A', 'B', 'C'],[ 'd', 'E', 'F'],[ 'G']]

我遇到的问题是,以前的元素会以某种方式在2D数组中写入后续条目,例如

[[ 'G'],[ 'G'],[ 'G']]

我已经仔细研究了但是看不出这是怎么回事,因为对2D数组的写入应该只针对每个新条目发生一次,然后它们应该只在将新数据附加到2D数组时发生,并且不会覆盖以前的条目。

我有点卡住了,你们有没有想过为什么会这样?

感谢:!)

代码

    class Reader

    def initialize
        @command_array = Array.new { Array.new } # 2D array

    end

      def run(file)
        return puts "please provide correct file" if file.nil? || !File.exists?(file)

        command_line = Array.new            #Temp array
        p "----------------------------------------------------------------"
        File.open(file).each do |line|     
          p "looking at a line of commands..."
          line.split(' ').each do |command| 
            p "storing the command #{command} in temp array"
            command_line.push(command)
            p command_line
          end

          p "Storing the temp array as an element in the 2d array..."
          @command_array.push(command_line)
          p @command_array

          p "Clearing the temp array..."
          p "----------------------------------------------------------------"
          command_line.clear
        end
      end
    end

输入文件

A B C
D E F
G

输出

    "looking at a line of commands..."
    "storing the command A in temp array"
    ["A"]
    "storing the command B in temp array"
    ["A", "B"]
    "storing the command C in temp array"
    ["A", "B", "C"]
    "Storing the temp array as an element in the 2d array..."
    [["A", "B", "C"]]
    "Clearing the temp array..."
    "----------------------------------------------------------------"
    "looking at a line of commands..."
    "storing the command D in temp array"
    ["D"]
    "storing the command E in temp array"
    ["D", "E"]
    "storing the command F in temp array"
    ["D", "E", "F"]
    "Storing the temp array as an element in the 2d array..."
    [["D", "E", "F"], ["D", "E", "F"]]
    "Clearing the temp array..."
    "----------------------------------------------------------------"
    "looking at a line of commands..."
    "storing the command G in temp array"
    ["G"]
    "Storing the temp array as an element in the 2d array..."
    [["G"], ["G"], ["G"]]
    "Clearing the temp array..."

1 个答案:

答案 0 :(得分:3)

问题是你已经尽力为你解析的每一行回收相同的数组。请记住,在Ruby中,Array#push将一个对象引用(指针)放到您正在推送的数组中,因此对该对象的任何修改都将影响对它的所有引用。

您的计划的最小形式是:

class Reader
  def initialize
    @command_array = [ ]
  end

  def run(file)
    @command_array = File.readlines(file).map do |line|
      line.chomp.split(' ')
    end
  end
end

您对Array.new { Array.new }的初始分配并不是真正有用,第二个参数是默认值,从未使用过,因为您只有push个内容。 Ruby中没有必要以这种方式强类型化。数组只是一个数组,哈希只是一个哈希,它们不需要以任何特定的形式初始化,以便以任何特定的方式使用。 array[0]['hash_key']可以在同一对象上与array[1][2]同时生效。

每当你遇到像克隆这样的对象出现问题时,它们会以某种方式纠缠并且更改为一个会影响其他对象,你可能会无意中使用同一个对象。要查看Ruby将您的数组视为使用的内容:

p @command_array.map(&:object_id)

将显示哪些对象标识符在那里。在你的情况下,他们都是相同的。