[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

loop over array with indices

Johannes Ahl-mann

5/23/2005 9:12:00 PM

hi,

i have the recurring problem that i want to loop over an array with
each/collect/... but (as an afterthought) need the index of the current
element.

for a while i have been using the "idiom":

array.zip((0..array.length).to_a).each {|a| ...}

but it really feels clunky, disfigures the source and feels like
something that would work really well in haskell, yet not in ruby ;-)
apart from looping with:

array.each_index {|i| a = array[i]; ...}

are there any nice, rubyesque ways of doing this?

Johannes

BTW: what's the difference for an array of doing "array.each_index" as
opposed to "array.length.times"?