Extracting elements from an array

Hello,

I have a question about Enumerable#grep or perhaps Enumerable#map.

I have an array and I want to extract the entries that follow certain
criteria. I’d like to do something like:

array.grep { |x| valid?(x) }
or
array.map { |x| valid(x) ? x : return }

valid?() returns true or false depending on whether the input meets
the criteria.

In my particular problem I have an array of the form:

[ [ 34, “something” ] , [ 2, “another”] , … ]

I wish to eliminate the entries whose numeric value is less than - say -
3. So something like:

array.map { |x| x < 3 ? return : x }

would be nice.

I haven’t figured out how to do this with either grep or map. Can anyone
offer some advice?

Thanks a lot,
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

Daniel Carrera wrote:

Hello,

I have a question about Enumerable#grep or perhaps Enumerable#map.

I have an array and I want to extract the entries that follow certain
criteria. I’d like to do something like:

array.grep { |x| valid?(x) }
or
array.map { |x| valid(x) ? x : return }

array.select { |x| valid?(x) }

array = Array[[0, “eliminate”], [1, “eliminate”], [5, “keep”], [1, “eliminate”],
[10, “keep”], [20, “keep”], [-4, “eliminate”], [100, “keep”]]

array.delete_if { |x| x[0] < 3 }
puts array

···

On Wed, 15 Jan 2003 08:33:01 +0900, Daniel Carrera wrote:

In my particular problem I have an array of the form:

[ [ 34, “something” ] , [ 2, “another”] , … ]

I wish to eliminate the entries whose numeric value is less than - say -
3. So something like: