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.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
traverse two-dimesional array in ruby
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password