Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Splitting an Array

Reply
Thread Tools

Splitting an Array

 
 
James Edward Gray II
Guest
Posts: n/a
 
      08-27-2004
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



 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      08-27-2004
>>>>> "J" == James Edward Gray <> writes:

J> I have and Array and I need to iterate over it, finding a subset of the
J> elements it contains. I want that subset in a new Array and I want the
J> 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





 
Reply With Quote
 
 
 
 
Austin Ziegler
Guest
Posts: n/a
 
      08-27-2004
On Fri, 27 Aug 2004 22:57:53 +0900, James Edward Gray II
<> 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


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
--
Austin Ziegler *
* Alternate:


 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      08-27-2004
On Aug 27, 2004, at 9:05 AM, ts wrote:

>>>>>> "J" == James Edward Gray <> writes:

>
> J> I have and Array and I need to iterate over it, finding a subset of
> the
> J> elements it contains. I want that subset in a new Array and I want
> the
> J> 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.

James Edward Gray II



 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-27-2004

"James Edward Gray II" <> schrieb im Newsbeitrag
news:0AA1504B-F833-11D8-B4C8-...
> On Aug 27, 2004, at 9:05 AM, ts wrote:
>
> >>>>>> "J" == James Edward Gray <> writes:

> >
> > J> I have and Array and I need to iterate over it, finding a subset of
> > the
> > J> elements it contains. I want that subset in a new Array and I want
> > the
> > J> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
splitting a string and put it into an array Kai Jaensch C++ 26 01-18-2004 07:46 AM
splitting a string and putting it into an array? Kai Jaensch C++ 3 01-15-2004 09:51 AM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Dibling C++ 0 07-19-2003 04:41 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? Mark C++ 0 07-19-2003 04:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Ericson C++ 0 07-19-2003 04:03 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57