Forum: Ruby Accessing a specific index in a nested array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
70461a4707c93b3bd7f7a750f1191444?d=identicon&s=25 Pieter Mans (feanix)
on 2007-07-11 22:27
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.
017e05d1a49ffa59ea03e149e7af720b?d=identicon&s=25 Chris Shea (chrisshea)
on 2007-07-11 23:00
(Received via mailing list)
On Jul 11, 2:27 pm, Pieter Mans <tybr...@gmail.com> wrote:
> => [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-forum.com/.

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
E0ed615bd6632dd23165e045e3c1df09?d=identicon&s=25 Florian Gross (Guest)
on 2007-07-11 23:05
(Received via mailing list)
> 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].
Cf7cd97cdc8ed7d4ae92965b24f0dfad?d=identicon&s=25 Stefan Rusterholz (apeiros)
on 2007-07-11 23:22
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
70461a4707c93b3bd7f7a750f1191444?d=identicon&s=25 Pieter Mans (feanix)
on 2007-07-11 23:51
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?
70461a4707c93b3bd7f7a750f1191444?d=identicon&s=25 Pieter Mans (feanix)
on 2007-07-11 23:53
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)
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2007-07-12 00:05
(Received via mailing list)
On 7/11/07, Pieter Mans <tybriel@gmail.com> wrote:
> >
> irb> test.coords[1][1] = 0
> => 0
> irb> test.coords
> => [["#", 0, "#", "#", "#"], ["#", 0, "#", "#", "#"], ["#", 0, "#", "#",
> "#"], ["#", 0, "#", "#", "#"], ["#", 0, "#", "#", "#"]]
>
> Any idea what I'm doing wrong?

Most likely you have the same object for each element of your outside
array.  You can check by doing this:

irb> test.coords[0] === test.coords[1]  #for you, this will probably
return true
=> true

Look at how your array is created.  There are a number of ways to do
it.  Here's one that's not sexy but works.

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

hth,
Todd
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2007-07-12 00:10
(Received via mailing list)
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) }
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2007-07-12 00:12
(Received via mailing list)
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
70461a4707c93b3bd7f7a750f1191444?d=identicon&s=25 Pieter Mans (feanix)
on 2007-07-12 15:33
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
This topic is locked and can not be replied to.