[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Array#each - getting each element and the index

Pat Maddox

1/20/2006 2:08:00 PM

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

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


6 Answers

James Gray

1/20/2006 2:14:00 PM

0

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


Robert Klemme

1/20/2006 2:14:00 PM

0

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

Chris Hulan

1/20/2006 3:01:00 PM

0

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

Pat Maddox

1/20/2006 3:06:00 PM

0

On 1/20/06, James Edward Gray II <james@grayproductions.net> 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 = %w[foo bar]
> > a.each { |val, index| .... } # val => 'foo', index => 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.


Daniel Harple

1/20/2006 3:13:00 PM

0

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


Marcin Mielzynski

1/20/2006 4:04:00 PM

0

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