Strange two dimensional array behaviour

Discussion in 'Ruby' started by Zsolnai Csaba, Apr 30, 2009.

  1. This code is supposed to read in a 9x9 table (sudoku table) from a
    simple text file. It then needs to write it out on the screen.

    #read from file

    def puzzle_beolvas fnev
    f = File.new(fnev,"r")
    puzzle = Array.new(9){[]}
    for i in (1..9)
    puzzle = Array.new(9)
    end
    i = 1

    f.each do |sor|
    j = 1
    sor.each do |str|
    puzzle[j] = str
    j = j + 1
    end
    #print "\n"
    i = i + 1
    end
    f.close

    #Write out the 2 dimensional array

    for i in (1..9)
    for j in (1..9)
    print puzzle[j]
    print " "
    end
    print "\n"
    end

    end

    #the program does this for every file, whose name is given as an
    argument

    ARGV.each do |a|
    puzzle_beolvas a
    end


    So, the output should be something like this:

    6 1 5 4 2 9 3 8 7
    4 7 2 8 5 3 1 6 9
    3 9 8 1 7 6 2 4 5
    7 4 6 2 3 1 5 9 8
    5 2 1 9 8 4 6 7 3
    9 8 3 7 6 5 4 1 2
    8 6 7 5 4 2 9 3 1
    1 5 4 3 9 7 8 2 6
    2 3 9 6 1 8 7 5 .

    Instead, i get this:

    6 1 5 4 2 9 3 8 7
    nil nil nil nil nil nil nil nil
    4 7 2 8 5 3 1 6 9
    nil nil nil nil nil nil nil nil
    3 9 8 1 7 6 2 4 5
    nil nil nil nil nil nil nil nil
    7 4 6 2 3 1 5 9 8
    nil nil nil nil nil nil nil nil
    5 2 1 9 8 4 6 7 3
    nil nil nil nil nil nil nil nil
    9 8 3 7 6 5 4 1 2
    nil nil nil nil nil nil nil nil
    8 6 7 5 4 2 9 3 1
    nil nil nil nil nil nil nil nil
    1 5 4 3 9 7 8 2 6
    nil nil nil nil nil nil nil nil
    2 3 9 6 1 8 7 5 .
    nil nil nil nil nil nil nil nil
    6 1 5 4 2 9 3 8 7



    Any ideas, why ?
    --
    Posted via http://www.ruby-forum.com/.
     
    Zsolnai Csaba, Apr 30, 2009
    #1
    1. Advertising

  2. On Apr 30, 2009, at 3:54 PM, Zsolnai Csaba wrote:

    > This code is supposed to read in a 9x9 table (sudoku table) from a
    > simple text file. It then needs to write it out on the screen.
    >
    > #Write out the 2 dimensional array
    >
    > for i in (1..9)
    > for j in (1..9)
    > print puzzle[j]
    > print " "
    > end
    > print "\n"
    > end
    >
    > end

    <snip>
    >
    > Any ideas, why ?
    > --



    Arrays index from 0. A 9-element array has indexes from 0..8

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
     
    Rob Biedenharn, Apr 30, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach
    Replies:
    0
    Views:
    436
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,928
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    657
    Vivek
    Aug 21, 2003
  4. Venkat
    Replies:
    4
    Views:
    975
    Venkat
    Dec 5, 2003
  5. Kenneth Brody

    Two-dimensional array to array of arrays

    Kenneth Brody, Dec 21, 2006, in forum: C Programming
    Replies:
    2
    Views:
    508
    Simon Biber
    Dec 21, 2006
Loading...

Share This Page