Splitting an Array

I have and Array and I need to iterate over it, finding a subset of the elements it contains. I want that subset in a new Array and I want the original Array altered so it no longer contains the subset.

The following code does what I want, but it seems kind of "clunky":

a = [ 1, 2, 3 ]
b = [ ] # so it exists outside the iterator

puts a.delete_if { |e| b << e if e > 1; e > 1; } # prints 1
puts b # prints 2 and 3

Is this the best way to go about it?

Thanks.

James Edward Gray II

I have and Array and I need to iterate over it, finding a subset of the
elements it contains. I want that subset in a new Array and I want the
original Array altered so it no longer contains the subset.

You can also use Enum#partition

   a, b = a.partition { |e| e <= 1}

Guy Decoux

It's still a bit clunky -- I can't think of a single operation to do
this offhand, but:

b = a.select { |e| e > 1 }
a.delete_if { |e| e > 1 }

-a

···

On Fri, 27 Aug 2004 22:57:53 +0900, James Edward Gray II <james@grayproductions.net> wrote:

I have and Array and I need to iterate over it, finding a subset of the
elements it contains. I want that subset in a new Array and I want the
original Array altered so it no longer contains the subset.

The following code does what I want, but it seems kind of "clunky":

a = [ 1, 2, 3 ]
b = # so it exists outside the iterator

puts a.delete_if { |e| b << e if e > 1; e > 1; } # prints 1
puts b # prints 2 and 3

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

Exactly what I was looking for. Thank you.

James Edward Gray II

···

On Aug 27, 2004, at 9:05 AM, ts wrote:

> I have and Array and I need to iterate over it, finding a subset of the
> elements it contains. I want that subset in a new Array and I want the
> original Array altered so it no longer contains the subset.

You can also use Enum#partition

   a, b = a.partition { |e| e <= 1}

"James Edward Gray II" <james@grayproductions.net> schrieb im Newsbeitrag
news:0AA1504B-F833-11D8-B4C8-000A95BA45F8@grayproductions.net...

>
> > I have and Array and I need to iterate over it, finding a subset of
> the
> > elements it contains. I want that subset in a new Array and I want
> the
> > original Array altered so it no longer contains the subset.
>
> You can also use Enum#partition
>
> a, b = a.partition { |e| e <= 1}

Exactly what I was looking for. Thank you.

Note, that partition returns two new arrays and leaves the original
unmodified. So for large arrays that might be a problem. That might or
might not be ok, depending on your situation.

Btw, your solution can be written a bit simpler:

a = [1,2,3]
b =
a.delete_if {|e| e > 1 and b << e}

IMHO that is one of the shortest of the solutions that need only two arrays.

If you know that the array is sorted and that there is at least one element
that will satisfy the condition, more options are possible:

a = [1,2,3]
b = a.slice!(a.each_with_index {|x,i| break i if x > 1} .. -1)

Kind regards

    robert

···

On Aug 27, 2004, at 9:05 AM, ts wrote: