Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Array#each - getting each element and the index

Reply
Thread Tools

Array#each - getting each element and the index

 
 
Pat Maddox
Guest
Posts: n/a
 
      01-20-2006
Is there a way to get each element of an array, as well the index of
that element? Something like
a =3D %w[foo bar]
a.each { |val, index| .... } # val =3D> 'foo', index =3D> 0

If there's no way to do that, is it better to use each_index and then
access the array?
a.each_index do |i|
puts i
puts a[i]
end

vs finding the index after you have the element
a.each do |val|
puts a.index(val)
puts val
end

Intuitively I'd think the second way may be a bit slower than the
first, because it has to search through the array for the element,
rather than accessing the index directly. Maybe not though...I'd just
like a bit of explanation on this.

Thanks,
Pat


 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      01-20-2006
On Jan 20, 2006, at 8:08 AM, Pat Maddox wrote:

> Is there a way to get each element of an array, as well the index of
> that element?


You bet.

> Something like
> a = %w[foo bar]
> a.each { |val, index| .... } # val => 'foo', index => 0


a.each_with_index ...

James Edward Gray II


 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      01-20-2006
Pat Maddox wrote:
> Is there a way to get each element of an array, as well the index of
> that element? Something like
> a = %w[foo bar]
> a.each { |val, index| .... } # val => 'foo', index => 0


a.each_with_index { |val, index| .... }

robert

 
Reply With Quote
 
ChrisH
Guest
Posts: n/a
 
      01-20-2006
Pat Maddox wrote:
> Is there a way to get each element of an array, as well the index of
> that element? Something like
> a = %w[foo bar]
> a.each { |val, index| .... } # val => 'foo', index => 0


Yes, and suprisingly it called each_with_index 9^)

Cheers

 
Reply With Quote
 
Pat Maddox
Guest
Posts: n/a
 
      01-20-2006
On 1/20/06, James Edward Gray II <> wrote:
> On Jan 20, 2006, at 8:08 AM, Pat Maddox wrote:
>
> > Is there a way to get each element of an array, as well the index of
> > that element?

>
> You bet.
>
> > Something like
> > a =3D %w[foo bar]
> > a.each { |val, index| .... } # val =3D> 'foo', index =3D> 0

>
> a.each_with_index ...
>
> James Edward Gray II
>
>


Perfect, thanks

Is this using some idiom (e.g. attaching _with_index) that I don't
know about? I didn't see that method in the Array rdoc.


 
Reply With Quote
 
Daniel Harple
Guest
Posts: n/a
 
      01-20-2006
On Jan 20, 2006, at 4:06 PM, Pat Maddox wrote:

> Is this using some idiom (e.g. attaching _with_index) that I don't
> know about? I didn't see that method in the Array rdoc.


If you do a ``ri Array'' you will see that #each_with_index is mixed
in from Enumerable. see Enumerable#each_with_index for more information.

-- Daniel


 
Reply With Quote
 
Marcin Mielżyński
Guest
Posts: n/a
 
      01-20-2006
Pat Maddox wrote:

>
> Is this using some idiom (e.g. attaching _with_index) that I don't
> know about? I didn't see that method in the Array rdoc.
>
>


It is a plain method name.

Enumerable#each_with_index

lopex
 
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
Transform a 2D color image into 2 images of (R1,G1,B) at each pixelof image 1 and (R2,G2,B) at each pixel of image2 for STEREO visualization 88888 Dihedral C++ 10 12-23-2011 02:28 PM
Hash .each and different action for each key Igor Nn Ruby 7 05-28-2011 12:33 PM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
getting index to parentNode's childNodes array given an element id yawnmoth Javascript 1 06-24-2006 09:19 AM



Advertisments