[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Accessing a specific index in a nested array

Pieter Mans

7/11/2007 8:28:00 PM

I'm trying to create a map datatype (a glorified matrix, really). It
basically works on having a bunch of nested arrays. The idea is that I
pass an X and a Y coord as arguments and then it returns the Xth element
in the Yth array.

The problem is that I cant seem to get a specific value from a nested
array.

For example, this works:
irb> array[2]
=> [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]

But this doesn't seem to:
irb> array[2[2]]
=> [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]

I'm pretty sure I'm just getting the syntax wrong.

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

8 Answers

Chris Shea

7/11/2007 8:56:00 PM

0

On Jul 11, 2:27 pm, Pieter Mans <tybr...@gmail.com> wrote:
> I'm trying to create a map datatype (a glorified matrix, really). It
> basically works on having a bunch of nested arrays. The idea is that I
> pass an X and a Y coord as arguments and then it returns the Xth element
> in the Yth array.
>
> The problem is that I cant seem to get a specific value from a nested
> array.
>
> For example, this works:
> irb> array[2]
> => [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
>
> But this doesn't seem to:
> irb> array[2[2]]
> => [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
>
> I'm pretty sure I'm just getting the syntax wrong.
>
> --
> Posted viahttp://www.ruby-....

035:0> a = [[1,2],[3,4]]
=> [[1, 2], [3, 4]]
036:0> a[1]
=> [3, 4]
037:0> a[1][0]
=> 3

Array#[] is just a method, so when you need to do it in succession,
you just add it do the end. Just like " hello ".strip.upcase

HTH,
Chris

Florian Groß

7/11/2007 9:02:00 PM

0

> For example, this works:
> irb> array[2]
> => [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
>
> But this doesn't seem to:
> irb> array[2[2]]
> => [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
>
> I'm pretty sure I'm just getting the syntax wrong.

Yup, you're calling [] on the number 2. You want array[2][2].


Stefan Rusterholz

7/11/2007 9:23:00 PM

0

Pieter Mans wrote:
> I'm trying to create a map datatype (a glorified matrix, really).

Just wanted to point out that ruby has a Matrix class in stdlib.

Regards
Stefan

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

Pieter Mans

7/11/2007 9:51:00 PM

0

Florian Gross wrote:
>> For example, this works:
>> irb> array[2]
>> => [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
>>
>> But this doesn't seem to:
>> irb> array[2[2]]
>> => [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
>>
>> I'm pretty sure I'm just getting the syntax wrong.
>
> Yup, you're calling [] on the number 2. You want array[2][2].

Thanks! That worked nicely. I'm getting a weird problem when setting
values tho. When I try to set the value of an item in a nested array, it
sets the value in all the other nested arrays too:

irb> test = Map.new(5,5)
=> #<Map:0x2dfd7dc @coords=[["#", "#", "#", "#", "#"], ["#", "#", "#",
"#", "#"], ["#", "#", "#", "#", "#"], ["#", "#", "#", "#", "#"], ["#"
, "#", "#", "#", "#"]]>
irb> test.coords[1][1] = 0
=> 0
irb> test.coords
=> [["#", 0, "#", "#", "#"], ["#", 0, "#", "#", "#"], ["#", 0, "#", "#",
"#"], ["#", 0, "#", "#", "#"], ["#", 0, "#", "#", "#"]]

Any idea what I'm doing wrong?


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

Pieter Mans

7/11/2007 9:53:00 PM

0

Stefan Rusterholz wrote:
> Pieter Mans wrote:
>> I'm trying to create a map datatype (a glorified matrix, really).
>
> Just wanted to point out that ruby has a Matrix class in stdlib.
>
> Regards
> Stefan

Yep, I've tried using it, but I wasn't really satisfied with it (for
reasons I'm not sure I really should go into at length at the risk of
derailing this thread)

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

Todd Benson

7/11/2007 10:10:00 PM

0

On 7/11/07, Todd Benson <caduceass@gmail.com> wrote:

> size = 5
> test.coords = []
> size.times { test.coords << Array.new size }

Oh, for cripe's sake. That last line should be:

size.times { test.coords << Arra.new(size) }

Todd Benson

7/11/2007 10:11:00 PM

0

On 7/11/07, Todd Benson <caduceass@gmail.com> wrote:
> On 7/11/07, Todd Benson <caduceass@gmail.com> wrote:
>
> > size = 5
> > test.coords = []
> > size.times { test.coords << Array.new size }
>
> Oh, for cripe's sake. That last line should be:
>
> size.times { test.coords << Arra.new(size) }

I give up. One more time all over again test.coords is arr below:

size = 5
arr = []
size.times { arr << Array.new(size) }

tested

sorry,
Todd

Pieter Mans

7/12/2007 1:34:00 PM

0

Todd Benson wrote:
> I give up. One more time all over again test.coords is arr below:
>
> size = 5
> arr = []
> size.times { arr << Array.new(size) }
>
> tested
>
> sorry,
> Todd

Thanks, you guys are awesome! :D

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