Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > loop over array with indices

Reply
Thread Tools

loop over array with indices

 
 
Johannes Ahlmann
Guest
Posts: n/a
 
      05-23-2005
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"?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Multiple indices in for loop mt C Programming 3 03-30-2011 04:03 AM
Using an array value as indices for an array KLaus Blank VHDL 1 07-07-2008 07:38 PM
VOIP over VPN over TCP over WAP over 3G Theo Markettos UK VOIP 2 02-14-2008 03:27 PM
deleting items within a for loop - mutable indices SnuSnu Python 6 04-25-2004 02:09 AM



Advertisments