Beginner question - n-dimensional arrays

Greetings!
I’m having a little problem with n-dimensional arrays. Here’s what I intend to do:

I want to create 2-dimensional array which I’m using as a grid for a simulation. The initialize method receives (among others) two values, one for the number of rows and one for the number of colums, which I store in @rows and @cols respectively. The intialize method also creates the lattice based on those values with the following code

@lattice = Array.new(@rows, Array.new(@cols))
k = 0
for i in 0…@rows
for j in 0…@cols
@lattice[i[j]] = k % 10
k = k + 1
end
end

I only use k for testing purposes - I expected to see a sequence of numbers from 0 to 9 repeating across the grid when I use

for i in 0…@rows
for j in 0…@cols
print @lattice[i[j]].to_s << " "
end
print "\n"
end

However, my results are far from that - I get something similar to this:

0 0 0 0 0 0 0
5 0 0 0 0 0 0
0 5 0 0 0 0 0
5 5 0 0 0 0 0
0 0 5 0 0 0 0
5 0 5 0 0 0 0
etc…

basically inverted binary number representations (the 5 and 0 are constant if I use mod 10… when I use mod 11 I get 6 and 8 instead). Could anyone help me out here? Where am I erring?

···


CWStrommer - New equipment FuBARing daily!


The NEW Netscape 7.0 browser is now available. Upgrade now! http://channels.netscape.com/ns/browsers/download.jsp

Get your own FREE, personal Netscape Mail account today at http://webmail.netscape.com/

If you’re doing anything numerical, you’ll probably want to use either
the Matrix class or take a look at NArray on RAA.

C. W. Strommer wrote:

···

Greetings!
I’m having a little problem with n-dimensional arrays. Here’s what I intend to do:

I want to create 2-dimensional array which I’m using as a grid for a simulation. The initialize method receives (among others) two values, one for the number of rows and one for the number of colums, which I store in @rows and @cols respectively. The intialize method also creates the lattice based on those values with the following code

@lattice = Array.new(@rows, Array.new(@cols))
k = 0
for i in 0…@rows
for j in 0…@cols
@lattice[i[j]] = k % 10
k = k + 1
end
end

I only use k for testing purposes - I expected to see a sequence of numbers from 0 to 9 repeating across the grid when I use

for i in 0…@rows
for j in 0…@cols
print @lattice[i[j]].to_s << " "
end
print “\n”
end

However, my results are far from that - I get something similar to this:

0 0 0 0 0 0 0
5 0 0 0 0 0 0
0 5 0 0 0 0 0
5 5 0 0 0 0 0
0 0 5 0 0 0 0
5 0 5 0 0 0 0
etc…

basically inverted binary number representations (the 5 and 0 are constant if I use mod 10… when I use mod 11 I get 6 and 8 instead). Could anyone help me out here? Where am I erring?

Greetings!
I'm having a little problem with n-dimensional arrays. Here's what I intend to do:

  I want to create 2-dimensional array which I'm using as a grid for a simulation. The initialize method receives (among others) two values, one for the number of rows and one for the number of colums, which I store in @rows and @cols respectively. The intialize method also creates the lattice based on those values with the following code

  @lattice = Array.new(@rows, Array.new(@cols))
  k = 0
  for i in 0...@rows
     for j in 0...@cols
        @lattice[i[j]] = k % 10
        k = k + 1
     end
  end

Problem 1: you need @lattice[i][j] not @lattice[i[j]]

Problem 2: Array.new(n, obj) creates an array containing n identical
references to the same obj, which is usually not want you want (unless obj
is immutable like a Fixnum or true/false/nil)

In devel versions of Ruby I understand you can do something like:

  @lattice = Array.new(@rows) { Array.new(@cols) }

where the block is run for each row, thus generating a new object.

Under 1.6.8 you need to do it a bit more explicitly, for example

   @lattice =
   @rows.times { @lattice << Array.new(@cols) }

or more compactly,

   @lattice = (0...@rows).collect { Array.new(@cols) }

  for i in 0...@rows
     for j in 0...@cols
        print @lattice[i[j]].to_s << " "
     end
     print "\n"
  end

The above works when you fix [i][j]. However in Ruby if you find yourself
writing explicit loops it usually means you've overlooked a more direct way
of performing the desired action :slight_smile: In this case you could use

    @lattice.each do |row|
      row.each do |elem|
        print elem.to_s << " "
      end
      puts
    end

Regards,

Brian.

···

On Thu, Feb 27, 2003 at 01:50:26AM +0900, C. W. Strommer wrote: