Multi-dimensional arrays

Discussion in 'Ruby' started by Graham Smith, Jul 7, 2006.

  1. Graham Smith

    Graham Smith Guest

    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
    Graham Smith, Jul 7, 2006
    #1
    1. Advertising

  2. 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
    Jake McArthur, Jul 7, 2006
    #2
    1. Advertising

  3. 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 = @default.call(i)
    end
    end
    rb_array_indexing(index)
    end
    end

    a = []
    a.default { [] }

    a[1][0] = "Hello"
    a #=> [[], ["Hello"]]
    Logan Capaldo, Jul 8, 2006
    #3
    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. Replies:
    2
    Views:
    8,656
    Jim Lewis
    Mar 21, 2006
  2. Alf P. Steinbach
    Replies:
    0
    Views:
    435
    Alf P. Steinbach
    Aug 18, 2003
  3. John Harrison
    Replies:
    4
    Views:
    6,923
    Default User
    Aug 19, 2003
  4. Icosahedron
    Replies:
    8
    Views:
    651
    Vivek
    Aug 21, 2003
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    200
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page