indices of non nil elements in an array

Discussion in 'Ruby' started by Patrick Doyle, Aug 25, 2008.

  1. 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
     
    Patrick Doyle, Aug 25, 2008
    #1
    1. Advertising

  2. On Sun, Aug 24, 2008 at 6:20 PM, 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.


    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
     
    Christopher Dicely, Aug 25, 2008
    #2
    1. Advertising

  3. 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
     
    Patrick Doyle, Aug 25, 2008
    #3
  4. Patrick Doyle

    Xavier Noria Guest

    On Mon, Aug 25, 2008 at 3:20 AM, 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?


    Not exactly what you ask, but in case it's actually what you are after
    have a look at Array#compact.
     
    Xavier Noria, Aug 25, 2008
    #4
  5. 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.nil? }

    or select for a 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!
     
    David A. Black, Aug 25, 2008
    #5
  6. 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
     
    Patrick Doyle, Aug 25, 2008
    #6
  7. On Mon, Aug 25, 2008 at 7:54 AM, David A. Black <> 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.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
     
    Patrick Doyle, Aug 25, 2008
    #7
  8. Patrick Doyle

    Guest

    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" <> wrote:

    > On Mon, Aug 25, 2008 at 7:54 AM, David A. Black <>
    > 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.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
    >
     
    , Aug 25, 2008
    #8
  9. On Mon, Aug 25, 2008 at 8:22 AM, <> 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
     
    Patrick Doyle, Aug 25, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candler

    puts nil generates "nil\n"

    Brian Candler, Nov 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    120
  2. John Carter
    Replies:
    64
    Views:
    676
    Klaus Stein
    May 19, 2005
  3. ako...

    a == nil or a.nil?

    ako..., Nov 22, 2005, in forum: Ruby
    Replies:
    6
    Views:
    155
    Douglas Livingstone
    Nov 23, 2005
  4. Christoffer Sawicki
    Replies:
    5
    Views:
    273
    Christoffer Sawicki
    Sep 2, 2006
  5. Eero Saynatkari

    nil.to_s != "nil"

    Eero Saynatkari, Oct 27, 2006, in forum: Ruby
    Replies:
    11
    Views:
    242
    Gregory Brown
    Oct 28, 2006
Loading...

Share This Page