[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

traverse two-dimesional array in ruby

aidy

6/28/2006 3:26:00 PM

Hi,

I know I can use the each method to traverse a one-dimensional array

my_array.each{|x|
p x
}

but how would I do a similar thing for a two dimensional array?

cheers

aidy

2 Answers

Tim Hoolihan

6/28/2006 3:30:00 PM

0

main_array.each{|subarray|
subarray.each { |elem|
puts elem
}
}

aidy wrote:
> Hi,
>
> I know I can use the each method to traverse a one-dimensional array
>
> my_array.each{|x|
> p x
> }
>
> but how would I do a similar thing for a two dimensional array?
>
> cheers
>
> aidy
>

Dumaiu

6/28/2006 5:17:00 PM

0

Once you have the 'nested each()' trick down, you realize that this
is basically what Array#flatten() does. As long as efficiency isn't
paramount, you can shortcut with 'array.flatten.each {}' for any depth.