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

On Sun, Aug 24, 2008 at 6:20 PM, Patrick D. [email protected]
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.

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

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

On Mon, Aug 25, 2008 at 3:20 AM, Patrick D. [email protected]
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?

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

Hi –

On Mon, 25 Aug 2008, Patrick D. 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

On Mon, Aug 25, 2008 at 7:54 AM, David A. Black [email protected]
wrote:

On Mon, 25 Aug 2008, Patrick D. 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

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

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 Mon, Aug 25, 2008 at 8:22 AM, [email protected] wrote:

That can be made even more elegant:

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs