Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Multi-dimensional arrays

Reply
Thread Tools

Multi-dimensional arrays

 
 
Graham Smith
Guest
Posts: n/a
 
      07-07-2006
Hi,

I have just started programming with Ruby and have run into the following
problem.

I'm trying to work with 3 dimensional arrays and have run into a problem
dimensioning the array.

a = [[[]]]
a[0][0] = [ 0,1,2,3]
a[0][1] = [ "zero", "one","two",three"]

The above assignments work without a problem, but the following assignment
fails.

a[1][0] = [ 4, 5, 6, 7]
NoMethodError: undefined method `[]=' for nil:NilClass

How do you dimension the number of elements in a multi-dimensional array when
the final number of elements in the array is not known.

--
Regards,

Graham Smith

 
Reply With Quote
 
 
 
 
Jake McArthur
Guest
Posts: n/a
 
      07-07-2006
On Jul 7, 2006, at 12:09 PM, Graham Smith wrote:

> How do you dimension the number of elements in a multi-dimensional
> array when
> the final number of elements in the array is not known.


Try http://raa.ruby-lang.org/project/narray/

- Jake McArthur

 
Reply With Quote
 
 
 
 
Logan Capaldo
Guest
Posts: n/a
 
      07-08-2006

On Jul 7, 2006, at 1:09 PM, Graham Smith wrote:

> Hi,
>
> I have just started programming with Ruby and have run into the
> following
> problem.
>
> I'm trying to work with 3 dimensional arrays and have run into a
> problem
> dimensioning the array.
>
> a = [[[]]]
> a[0][0] = [ 0,1,2,3]
> a[0][1] = [ "zero", "one","two",three"]
>
> The above assignments work without a problem, but the following
> assignment
> fails.
>
> a[1][0] = [ 4, 5, 6, 7]
> NoMethodError: undefined method `[]=' for nil:NilClass
>
> How do you dimension the number of elements in a multi-dimensional
> array when
> the final number of elements in the array is not known.
>
> --
> Regards,
>
> Graham Smith
>


class Array
def default(&proc)
@default = proc
self
end

alias_method :rb_array_indexing, :"[]"
def [](index)
if @default and index.kind_of? Integer and index >= length
index.downto(length) do |i|
self[i] = @default.call(i)
end
end
rb_array_indexing(index)
end
end

a = []
a.default { [] }

a[1][0] = "Hello"
a #=> [[], ["Hello"]]








 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
char arrays and integer arrays... why the difference? Bill Reyn C++ 3 06-22-2004 12:01 PM
Arrays.asList() returning java.util.Arrays$ArrayList Alexandra Stehman Java 5 06-17-2004 06:04 PM
Arrays and Pointers to Arrays kelvSYC C Programming 2 09-26-2003 06:52 AM
initializing arrays of arrays Mantorok Redgormor C Programming 4 09-11-2003 02:08 AM



Advertisments