Printing the Contents of a 2d Array

At the risk of sounding like a nuby to ruby…

I know in Python, I can print the contents of the array
[[1,2,3],[4,5,6],[7,8,9]] on the screen by using nested loops and "\n"
statements. I can make it appear as 3 rows of 3 elements, like a
matrix. I tried doing this in Ruby using analogous syntax:

[0…2].each do |i|
[0…2].each do |j|
print $a[i][j]
end
print "\n"
end

My output is 123456789–not what I’m looking for. Any thoughts?

How about

[[1,2,3],[4,5,6],[7,8,9]].each { |arr| puts arr.join(‘,’) }

···

On Thu, Oct 03, 2002 at 04:43:24AM +0900, Rotfeast wrote:

At the risk of sounding like a nuby to ruby…

I know in Python, I can print the contents of the array
[[1,2,3],[4,5,6],[7,8,9]] on the screen by using nested loops and “\n”
statements. I can make it appear as 3 rows of 3 elements, like a
matrix. I tried doing this in Ruby using analogous syntax:

[0…2].each do |i|
[0…2].each do |j|
print $a[i][j]
end
print “\n”
end

My output is 123456789–not what I’m looking for. Any thoughts?


Alan Chen
Digikata LLC
http://digikata.com

“Rotfeast” rotfeast68@hotmail.com wrote in message

[0…2].each do |i|
[0…2].each do |j|
print $a[i][j]
end
print “\n”
end

My output is 123456789–not what I’m looking for. Any thoughts?

I must admit it looks strange, the following is also surprising:

irb(main):032:0> def test2
irb(main):033:1> [0…2].each do print “x”; [1…2].each do print “y” end;
print “\n”; end
irb(main):034:1> end
nil
irb(main):035:0> test2
xy
[0…2]

I’m using Ruby 1.7.2 on Windows

Mikkel

Hi,

That’s because you are mixing array and range, which are totally different
objects. I think what you want is

$a = [[1,2,3],[4,5,6],[7,8,9]]
(0…2).each do |i| # not brackets, but grouping parentheses
(0…2).each do |j| # not brackets, but grouping parentheses
print $a[i][j]
end
print “\n”
end

0…2 # => a range
[0…2] # => an array with a single element of Range class

Regards,

Bill

···

=========================================================================
Rotfeast rotfeast68@hotmail.com wrote:

At the risk of sounding like a nuby to ruby…

I know in Python, I can print the contents of the array
[[1,2,3],[4,5,6],[7,8,9]] on the screen by using nested loops and “\n”
statements. I can make it appear as 3 rows of 3 elements, like a
matrix. I tried doing this in Ruby using analogous syntax:

[0…2].each do |i|
[0…2].each do |j|
print $a[i][j]
end
print “\n”
end

My output is 123456789–not what I’m looking for. Any thoughts?

Hello William,

Thursday, October 03, 2002, 12:23:47 AM, you wrote:

(0…2).each do |i| # not brackets, but grouping parentheses

0…2 # =>> a range
[0…2] # =>> an array with a single element of Range class

good topic for F.A. Pitfalls list

···


Best regards,
Bulat mailto:bulatz@integ.ru

Thanks to everyone who responded.

I had a feeling that the problem was something simple like this. I
had the same brackets/parentheses issue in Python (lists/tuples), but
your explanation cleared this up Bill. Thanks!

William Djaja Tjokroaminata billtj@y.glue.umd.edu wrote in message news:anfkf9$7ob$4@grapevine.wam.umd.edu

Hi,

That’s because you are mixing array and range, which are totally different
objects. I think what you want is

$a = [[1,2,3],[4,5,6],[7,8,9]]
(0…2).each do |i| # not brackets, but grouping parentheses
(0…2).each do |j| # not brackets, but grouping parentheses
print $a[i][j]
end
print “\n”
end

0…2 # => a range
[0…2] # => an array with a single element of Range class

Regards,

Bill

–snip–