Indices of non nil elements in an array

a = []
a[10] = "foo"

a.nitems returns a count of the non-nil, items. What is the simplest
way to return the indices of "a" that are not nil?

I'm sure there is some simple elegant way to do this, but I can't figure it out.

--wpd

If you need this frequently, you can monkeypatch Array to offer this
functionality; like Array#detect but gathers the indexes instead of
the values, and gathers indexes of non-nil values where no block is
given:

class Array
  def detect_indices
    result =
    each_with_index { |val, idx| (block_given? ? yield(val) : (not
val.nil?)) && result << idx }
    result
  end
end

···

On Sun, Aug 24, 2008 at 6:20 PM, Patrick Doyle <wpdster@gmail.com> wrote:

a =
a[10] = "foo"

a.nitems returns a count of the non-nil, items. What is the simplest
way to return the indices of "a" that are not nil?

I'm sure there is some simple elegant way to do this, but I can't figure it out.

Not exactly what you ask, but in case it's actually what you are after
have a look at Array#compact.

···

On Mon, Aug 25, 2008 at 3:20 AM, Patrick Doyle <wpdster@gmail.com> wrote:

a =
a[10] = "foo"

a.nitems returns a count of the non-nil, items. What is the simplest
way to return the indices of "a" that are not nil?

Hi --

···

On Mon, 25 Aug 2008, Patrick Doyle wrote:

a =
a[10] = "foo"

a.nitems returns a count of the non-nil, items. What is the simplest
way to return the indices of "a" that are not nil?

I'm sure there is some simple elegant way to do this, but I can't figure it out.

Try this:

(0...a.size).reject {|i| a[i].nil? }

or select for a[i] if you don't mind losing false as well as nil.

David

--
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
   Advancing with Rails January 19-22 Fort Lauderdale, FL
See http://www.rubypal.com for details and updates!

Thanks. That looks workable. What I could also do (I decided in the
mean time) is to change my array into a hash whose keys happen to be
integers. That way I could look at things like a.size and
a.keys.sort.

--wpd

Thanks. I tried that and realized that I lost all of the information
about the indices in so doing. As I've thought about this some more
and have realized that the index of the non-nil elements is just as
important to me as the non-nil elements themselves, I've decided that
using a Hash is probably a better solution to my needs than using an
Array.

--wpd

Ahhh -- that's the simple elegant way for which I was searching. I
kept going down the path of a.each_index and didn't like where that
was going.

--wpd

···

On Mon, Aug 25, 2008 at 7:54 AM, David A. Black <dblack@rubypal.com> wrote:

On Mon, 25 Aug 2008, Patrick Doyle wrote:

I'm sure there is some simple elegant way to do this, but I can't figure
it out.

Try this:

(0...a.size).reject {|i| a[i].nil? }

That can be made even more elegant:

a.reject (/e/ e.nil?)

Apologies I am on an iPod so the brackets should be braces and the slashes pipes!

···

Sent from my iPod

On Aug 25, 2008, at 1:04 PM, "Patrick Doyle" <wpdster@gmail.com> wrote:

On Mon, Aug 25, 2008 at 7:54 AM, David A. Black <dblack@rubypal.com> > wrote:

On Mon, 25 Aug 2008, Patrick Doyle wrote:

I'm sure there is some simple elegant way to do this, but I can't figure
it out.

Try this:

(0...a.size).reject {|i| a[i].nil? }

Ahhh -- that's the simple elegant way for which I was searching. I
kept going down the path of a.each_index and didn't like where that
was going.

--wpd

But that won't return the indices of the the non-nil elements, instead
it extracts the non-nil elements from the array (which is the same
thing that Array#compact) does.

--wpd

···

On Mon, Aug 25, 2008 at 8:22 AM, <buddhamagnet@gmail.com> wrote:

That can be made even more elegant:

a.reject (/e/ e.nil?)