Splitting an Array

Discussion in 'Ruby' started by James Edward Gray II, Aug 27, 2004.

  1. 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
    James Edward Gray II, Aug 27, 2004
    #1
    1. Advertising

  2. James Edward Gray II

    ts Guest

    >>>>> "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
    ts, Aug 27, 2004
    #2
    1. Advertising

  3. 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:
    Austin Ziegler, Aug 27, 2004
    #3
  4. 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
    James Edward Gray II, Aug 27, 2004
    #4
  5. "James Edward Gray II" <> schrieb im Newsbeitrag
    news:...
    > 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
    Robert Klemme, Aug 27, 2004
    #5
    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. John Ericson
    Replies:
    0
    Views:
    416
    John Ericson
    Jul 19, 2003
  2. Mark
    Replies:
    0
    Views:
    428
  3. John Dibling
    Replies:
    0
    Views:
    399
    John Dibling
    Jul 19, 2003
  4. Kai Jaensch
    Replies:
    26
    Views:
    706
    Jerry Coffin
    Jan 18, 2004
  5. Kai Jaensch
    Replies:
    3
    Views:
    426
    Kajoka
    Jan 15, 2004
Loading...

Share This Page