[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Multi-dimensional arrays

Graham Smith

7/7/2006 5:09:00 PM

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

3 Answers

Jake McArthur

7/7/2006 5:35:00 PM

0

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/proje...

- Jake McArthur

Logan Capaldo

7/8/2006 4:56:00 AM

0


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"]]








John Radgosky

6/23/2009 10:15:00 PM

0

On Jun 23, 4:58 pm, MikeRyder <n...@nospam.com> wrote:
> On Tue, 23 Jun 2009 09:29:32 -0700 (PDT), JRad <jradgo...@yahoo.com>
> wrote:
>
>
>
>
>
> >On Jun 23, 11:12 am, "Pieter" <hrdou...@zonnet.nl> wrote:
> >> On 23 jun, 15:19, JRad <jradgo...@yahoo.com> wrote:
>
> >> Hi John,
>
> >> Thank you very much for your post.
> >> There is one paragraph I answer to;
> >> that's why I snipped the rest.
>
> >> > Fact is, the author knows what to do and how with Mike.  If you are
> >> > being guided then I stand corrected, by all means.
> >> > I am guided.
>
> >> > If you are living
> >> > the course then you are wasting your time.
>
> >> - Living the Course never is wasting time!
>
> >I couldn't agree more.  But with MikeR are you really living it to the
> >full ?  
>
> If you're not living it 'to the full', rat-boy, if you're just buying
> into your ego demands that you kill the messenger because it wants to
> turn a deaf ear to what I'm saying, you dont get it.- Hide quoted text -
>
> - Show quoted text -

now, now Mikeypooh ....you're not trying to lecture me again, are
you ? The course version of the 7th Day Adventist or Mormon knocking
on my door wanting to tell me what I lack ?

Trust the author.

If I lack anything he will identify it and it will be supplied or
rather, is already supplied through God's abundance and is already
with me and I just haven't awoken to it. And when I am supposed to,
the author will waken me to what it is I am supposed to become
aware.

You're not still insisting you can compete with the author , are
you ? It seems you are which means you can only be one stupid
asshole. Now get back into the ward, meds time is approaching. Nut
job. Your persistence with nothing indicates you are a total nut
job. So excuse me while I ignore your stupid ass for the remainder of
this day, ok witchew ? Dubmass as thick as two times two planks.

You're not the messenger, stupid, the author is. You're just a fake
wannabe. And a deaf dumb and blind one to boot. Pest. Where IS that
pest killer ?

Peace

JR