# Indices of non nil elements in an array

**URL:** <https://rubytalk.org/t/indices-of-non-nil-elements-in-an-array/48584>\
**Category:** ruby-talk\
**Created:** [25 August 2008 01:20 UTC](https://rubytalk.org/t/indices-of-non-nil-elements-in-an-array/48584 "2008-08-25T01:20:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick\_Doyle](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Patrick\_Doyle](https://rubytalk.org/u/Patrick_Doyle)\
**Post date:** [25 August 2008 01:20 UTC](https://rubytalk.org/t/indices-of-non-nil-elements-in-an-array/48584/1 "2008-08-25T01:20:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Christopher\_Dicely](https://avatars.discourse-cdn.com/v4/letter/c/7c8e57/32.png) [@Christopher\_Dicely](https://rubytalk.org/u/Christopher_Dicely)\
**Post date:** [25 August 2008 02:14 UTC](https://rubytalk.org/t/indices-of-non-nil-elements-in-an-array/48584/2 "2008-08-25T02:14:16Z")

</div>

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  
&nbsp;&nbsp;def detect\_indices  
&nbsp;&nbsp;&nbsp;&nbsp;result =   
&nbsp;&nbsp;&nbsp;&nbsp;each\_with\_index { |val, idx| (block\_given? ? yield(val) : (not  
val.nil?)) && result \<\< idx }  
&nbsp;&nbsp;&nbsp;&nbsp;result  
&nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)\
**Post date:** [25 August 2008 11:38 UTC](https://rubytalk.org/t/indices-of-non-nil-elements-in-an-array/48584/3 "2008-08-25T11:38:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [25 August 2008 11:54 UTC](https://rubytalk.org/t/indices-of-non-nil-elements-in-an-array/48584/4 "2008-08-25T11:54:56Z")

</div>

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:  
> &nbsp;&nbsp;&nbsp;Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL  
> &nbsp;&nbsp;&nbsp;Advancing with Rails January 19-22 Fort Lauderdale, FL  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![Patrick\_Doyle](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Patrick\_Doyle](https://rubytalk.org/u/Patrick_Doyle)\
**Post date:** [25 August 2008 11:34 UTC](https://rubytalk.org/t/indices-of-non-nil-elements-in-an-array/48584/5 "2008-08-25T11:34:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Patrick\_Doyle](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Patrick\_Doyle](https://rubytalk.org/u/Patrick_Doyle)\
**Post date:** [25 August 2008 12:02 UTC](https://rubytalk.org/t/indices-of-non-nil-elements-in-an-array/48584/6 "2008-08-25T12:02:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Patrick\_Doyle](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Patrick\_Doyle](https://rubytalk.org/u/Patrick_Doyle)\
**Post date:** [25 August 2008 12:04 UTC](https://rubytalk.org/t/indices-of-non-nil-elements-in-an-array/48584/7 "2008-08-25T12:04:12Z")

</div>

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? }

---

<div class="post-metadata">

**Author:** ![Dave\_Goodchild](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dave\_Goodchild](https://rubytalk.org/u/Dave_Goodchild)\
**Post date:** [25 August 2008 12:22 UTC](https://rubytalk.org/t/indices-of-non-nil-elements-in-an-array/48584/8 "2008-08-25T12:22:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Patrick\_Doyle](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Patrick\_Doyle](https://rubytalk.org/u/Patrick_Doyle)\
**Post date:** [25 August 2008 12:38 UTC](https://rubytalk.org/t/indices-of-non-nil-elements-in-an-array/48584/9 "2008-08-25T12:38:25Z")

</div>

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?)
