[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

creating a 2d array

Adam Akhtar

2/2/2008 5:56:00 PM

Hi im just coming to ruby from C albeit after many years of no
programming. Im trying the simple matrix problem to get used to dealing
with arrays in ruby.

How do you create a blank 2d array?

I want to ask the user for the number of rows and columns and then
create a blank 2d array using those dimensions. From there I'd populate
it with the users desired data.

Cheers
--
Posted via http://www.ruby-....

8 Answers

Todd Benson

2/2/2008 9:02:00 PM

0

On Feb 2, 2008 11:55 AM, Adam Akhtar <adamtemporary@gmail.com> wrote:
> Hi im just coming to ruby from C albeit after many years of no
> programming. Im trying the simple matrix problem to get used to dealing
> with arrays in ruby.
>
> How do you create a blank 2d array?
>
> I want to ask the user for the number of rows and columns and then
> create a blank 2d array using those dimensions. From there I'd populate
> it with the users desired data.
>
> Cheers

x, y = 2, 3 #you get these values from the user
m = [] #initializing m for scope reasons
x.times { m << Array.new( y ) } # adding new arrays to m

That should get you started. Keep in mind that arrays are just
ordered lists of objects. You can have an ordered list of any object
(including other arrays). The [] index call can be chained. So to
access this 2d array at 0,0 you would do m[0][0].

hth,
Todd

Adam Akhtar

2/4/2008 11:46:00 AM

0

thanks that does help indeed.

--
Posted via http://www.ruby-....

ThoML

2/4/2008 12:13:00 PM

0

> x, y = 2, 3 #you get these values from the user
> m = [] #initializing m for scope reasons
> x.times { m << Array.new( y ) } # adding new arrays to m

Or maybe:

Array.new(x) {Array.new(y)}

Cameron McBride

2/4/2008 12:18:00 PM

0

On Feb 2, 2008 12:55 PM, Adam Akhtar <adamtemporary@gmail.com> wrote:
> Hi im just coming to ruby from C albeit after many years of no
> programming. Im trying the simple matrix problem to get used to dealing
> with arrays in ruby.

There is a matrix class in the stdlib. But if you're doing anything
numeric, I'd recommend using NArray.

> How do you create a blank 2d array?
>
> I want to ask the user for the number of rows and columns and then
> create a blank 2d array using those dimensions. From there I'd populate
> it with the users desired data.

Todd's suggestion will work, but I prefer:
rows, cols = 2,3
mat = Array.new(rows) { Array.new(cols) }

Cameron

Adam Akhtar

2/5/2008 9:35:00 PM

0


>
> Todd's suggestion will work, but I prefer:
> rows, cols = 2,3
> mat = Array.new(rows) { Array.new(cols) }
>
> Cameron

Why do you use the {}? Arent they used when your creating hashes.
--
Posted via http://www.ruby-....

Clifford Heath

2/5/2008 10:12:00 PM

0

Adam Akhtar wrote:
>> Todd's suggestion will work, but I prefer:
>> rows, cols = 2,3
>> mat = Array.new(rows) { Array.new(cols) }
>>
>> Cameron
>
> Why do you use the {}? Arent they used when your creating hashes.

This is a block (like do...end), not a Hash, in this context.
The block is passed to the Array constructor and which calls it
with each index to yield a value for the elements of the outer
array being constructed.

Clifford Heath.

Adam Akhtar

2/5/2008 11:10:00 PM

0

Ok i think i kinda understand. Could you tell me if this is something
specific to arrays or is it used for other objects as well? If I wanted
to learn more about this in a book what should i look up in the
index/contents?

Your help is much apppreciated!!




--
Posted via http://www.ruby-....

Clifford Heath

2/5/2008 11:57:00 PM

0

Adam Akhtar wrote:
> Ok i think i kinda understand. Could you tell me if this is something
> specific to arrays or is it used for other objects as well? If I wanted
> to learn more about this in a book what should i look up in the
> index/contents?

Worth reading in detail: <http://ruby-doc.org/core/classes/Arra....
Use IRB to enter examples of each usage shown, to make sure you understand
it. When you've read that, move on to Hash, String and Enumerable:

<http://ruby-doc.org/core/classes/Has...,
<http://ruby-doc.org/core/classes/Strin...,
<http://ruby-doc.org/core/classes/Enumerabl....

Apart from knowing the Ruby syntax, 90% of all your code is covered by
those four pages. When you've worked through them all once, do it again
from the top, and you'll learn still more.

Clifford Heath.