indices of non nil elements in an array

P

Patrick Doyle

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
 
C

Christopher Dicely

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
 
P

Patrick Doyle

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
 
X

Xavier Noria

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.
 
D

David A. Black

Hi --

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
 
P

Patrick Doyle

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
 
P

Patrick Doyle

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
 
B

buddhamagnet

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

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
 
P

Patrick Doyle

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top