How to remove empty element in an array

Discussion in 'Ruby' started by Li Chen, Oct 23, 2006.

  1. Li Chen

    Li Chen Guest

    Hi all,

    I have an array of [1,2,''] I want change it to [1,2]. I check the
    document about Array but I can't find a way to remove the empty element.
    Any comments?

    Thanks,

    Li

    --
    Posted via http://www.ruby-forum.com/.
    Li Chen, Oct 23, 2006
    #1
    1. Advertising

  2. On 23/10/06, Li Chen <> wrote:
    > Hi all,
    >
    > I have an array of [1,2,''] I want change it to [1,2]. I check the
    > document about Array but I can't find a way to remove the empty element.
    > Any comments?
    >
    > Thanks,
    >
    > Li
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    If you only want to get rid of empty strings
    array.reject{|element| element.empty?}

    If you want to get rid of nils
    array.compact

    Farrel
    Farrel Lifson, Oct 23, 2006
    #2
    1. Advertising

  3. On Monday 23 October 2006 14:27, Li Chen wrote:
    > Hi all,
    >
    > I have an array of [1,2,''] I want change it to [1,2]. I check the
    > document about Array but I can't find a way to remove the empty element.
    > Any comments?
    >
    > Thanks,
    >
    > Li


    You can Array#reject! if you want to do it in-place or Array#delete_if if you
    want to capture the non-empty elements on a new Array instance.

    Cheers
    Ulisses Montenegro

    --
    "The reasonable man adapts himself to the world; the unreasonable one persists
    in trying to adapt the world to himself. Therefore all progress depends on
    the unreasonable man."
    (George Bernard Shaw)
    Ulisses Reina Montenegro de Albuquerque, Oct 23, 2006
    #3
  4. Farrel Lifson wrote:
    > On 23/10/06, Li Chen <> wrote:
    >> Hi all,
    >>
    >> I have an array of [1,2,''] I want change it to [1,2]. I check the

    ...
    > If you only want to get rid of empty strings
    > array.reject{|element| element.empty?}


    array.reject{|element| element.empty? rescue false}

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Oct 23, 2006
    #4
  5. Li Chen

    Li Chen Guest

    Joel VanderWerf wrote:
    > Farrel Lifson wrote:
    >> On 23/10/06, Li Chen <> wrote:
    >>> Hi all,
    >>>
    >>> I have an array of [1,2,''] I want change it to [1,2]. I check the

    > ...
    >> If you only want to get rid of empty strings
    >> array.reject{|element| element.empty?}

    >
    > array.reject{|element| element.empty? rescue false}


    Hi Joel,

    I come out with my own solution by using a regxp:

    a=[1,2,'','']
    a.delete_if {|x| x=~/$/}
    p a

    ##output
    C:\Ruby\self>array3.rb
    [1, 2]

    --
    Posted via http://www.ruby-forum.com/.
    Li Chen, Oct 23, 2006
    #5
  6. Li Chen

    Pete Yandell Guest

    On 24/10/2006, at 4:02 AM, Li Chen wrote:

    > I come out with my own solution by using a regxp:
    >
    > a=[1,2,'','']
    > a.delete_if {|x| x=~/$/}
    > p a


    Your solution is broken. It'll delete any string from the array:

    irb(main):001:0> a = [1, 2, 'a', 'b']
    => [1, 2, "a", "b"]
    irb(main):002:0> a.delete_if {|x| x =~ /$/}
    => [1, 2]


    Pete Yandell
    http://notahat.com/
    Pete Yandell, Oct 24, 2006
    #6
  7. Li Chen

    Li Chen Guest

    Pete Yandell wrote:

    > Your solution is broken. It'll delete any string from the array:
    >
    > irb(main):001:0> a = [1, 2, 'a', 'b']
    > => [1, 2, "a", "b"]
    > irb(main):002:0> a.delete_if {|x| x =~ /$/}
    > => [1, 2]


    Hi,

    I think this time it should work: regxp for the empty sapce is ^\d*$.

    irb(main):003:0> a=[1,2,'','', 'a','b']
    => [1, 2, "", "", "a", "b"]
    irb(main):004:0> a.delete_if {|x| x=~/^\d*$/}
    => [1, 2, "a", "b"]


    Li

    --
    Posted via http://www.ruby-forum.com/.
    Li Chen, Oct 24, 2006
    #7
  8. On 10/23/06, Li Chen <> wrote:
    > Hi,
    >
    > I think this time it should work: regxp for the empty sapce is ^\d*$.
    >
    > irb(main):003:0> a=[1,2,'','', 'a','b']
    > => [1, 2, "", "", "a", "b"]
    > irb(main):004:0> a.delete_if {|x| x=~/^\d*$/}
    > => [1, 2, "a", "b"]


    I'm sorry, isn't \d used for a digit?

    irb(main):006:0> a = [1, '', 'a', '2', '3456']
    => [1, "", "a", "2", "3456"]
    irb(main):007:0> a.delete_if{|x| x =~ /^\d*$/}
    => [1, "a"]

    This one will delete any string that is made of numbers only.

    Cheers,
    Alvim.
    Marcelo Alvim, Oct 24, 2006
    #8
  9. Li Chen

    Seth E. Guest

    Li Chen wrote:

    > I have an array of [1,2,''] I want change it to [1,2]. I check the
    > document about Array but I can't find a way to remove the empty element.
    > Any comments?


    How about this?

    irb(main):201:0> arr = [1,2,'']
    => [1, 2, ""]
    irb(main):202:0> arr -= ['']
    => [1, 2]


    --
    Posted via http://www.ruby-forum.com/.
    Seth E., Oct 24, 2006
    #9
  10. Li Chen

    Pete Yandell Guest

    On 24/10/2006, at 1:02 PM, Seth E. wrote:

    > Li Chen wrote:
    >
    >> I have an array of [1,2,''] I want change it to [1,2]. I check the
    >> document about Array but I can't find a way to remove the empty
    >> element.
    >> Any comments?

    >
    > How about this?
    >
    > irb(main):201:0> arr = [1,2,'']
    > => [1, 2, ""]
    > irb(main):202:0> arr -= ['']
    > => [1, 2]


    If you just want to delete empty strings (as opposed to strings
    containing only whitespace), this would seem the obvious way:

    irb(main):003:0> a = [1, 2, '', 3, '']
    => [1, 2, "", 3, ""]
    irb(main):004:0> a.delete ''
    => ""
    irb(main):005:0> a
    => [1, 2, 3]

    If you want to delete any whitespace-only strings as well:

    irb(main):002:0> a = [1, 2, '', 3, ' ']
    => [1, 2, "", 3, " "]
    irb(main):007:0> a.delete_if {|s| s =~ /^\s*$/ }
    => [1, 2, 3]

    Pete Yandell
    http://notahat.com/
    Pete Yandell, Oct 24, 2006
    #10
  11. Pete Yandell wrote:
    > Your solution is broken. It'll delete any string from the array:
    >
    > irb(main):001:0> a = [1, 2, 'a', 'b']
    > => [1, 2, "a", "b"]
    > irb(main):002:0> a.delete_if {|x| x =~ /$/}
    > => [1, 2]
    > Pete Yandell
    > http://notahat.com/

    Thanks! I was beginning to think I was the only one seeing that
    behaviour. I feel much better now.

    --
    Posted via http://www.ruby-forum.com/.
    Patrick Spence, Oct 24, 2006
    #11
  12. On Tuesday 24 October 2006 00:02, Seth E. wrote:
    > Li Chen wrote:
    > > I have an array of [1,2,''] I want change it to [1,2]. I check the
    > > document about Array but I can't find a way to remove the empty element.
    > > Any comments?

    >
    > How about this?
    >
    > irb(main):201:0> arr = [1,2,'']
    > => [1, 2, ""]
    > irb(main):202:0> arr -= ['']
    > => [1, 2]


    <off-topic>
    I am new to the list, and have so far tried to keep myself as a silent lurker
    hidden on the corner, but coming from a Perl background (5+ years) and seeing
    this beautiful piece of code really makes me feel confident about leaving old
    habits and joining the Ruby club.
    </off-topic>

    --
    "The reasonable man adapts himself to the world; the unreasonable one persists
    in trying to adapt the world to himself. Therefore all progress depends on
    the unreasonable man."
    (George Bernard Shaw)
    Ulisses Reina Montenegro de Albuquerque, Oct 24, 2006
    #12
    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

    empty/non-empty element

    John, Jul 15, 2003, in forum: XML
    Replies:
    1
    Views:
    1,024
    Klaus Johannes Rusch
    Jul 16, 2003
  2. Lukas
    Replies:
    3
    Views:
    805
    spiff
    Nov 10, 2005
  3. Replies:
    4
    Views:
    2,528
  4. HANM
    Replies:
    2
    Views:
    708
    Joseph Kesselman
    Jan 29, 2008
  5. Li Chen
    Replies:
    2
    Views:
    102
    Austin Ziegler
    Oct 23, 2006
Loading...

Share This Page