Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to remove empty element in an array

Reply
Thread Tools

How to remove empty element in an array

 
 
Li Chen
Guest
Posts: n/a
 
      10-23-2006
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/.

 
Reply With Quote
 
 
 
 
Farrel Lifson
Guest
Posts: n/a
 
      10-23-2006
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

 
Reply With Quote
 
 
 
 
Ulisses Reina Montenegro de Albuquerque
Guest
Posts: n/a
 
      10-23-2006
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)

 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      10-23-2006
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

 
Reply With Quote
 
Li Chen
Guest
Posts: n/a
 
      10-23-2006
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/.

 
Reply With Quote
 
Pete Yandell
Guest
Posts: n/a
 
      10-23-2006

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/

 
Reply With Quote
 
Li Chen
Guest
Posts: n/a
 
      10-24-2006
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/.

 
Reply With Quote
 
Marcelo Alvim
Guest
Posts: n/a
 
      10-24-2006
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.

 
Reply With Quote
 
Seth E.
Guest
Posts: n/a
 
      10-24-2006
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/.

 
Reply With Quote
 
Pete Yandell
Guest
Posts: n/a
 
      10-24-2006

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/

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to remove empty element in an array Li Chen Ruby 2 10-23-2006 05:36 PM
Remove parent element with a child element matching a given rule patrizio.trinchini@googlemail.com XML 4 08-22-2006 11:31 AM
Altova Mapforce - xml 2 xml map: empty elements output although input element is not empty Lukas XML 3 11-10-2005 02:25 PM
empty/non-empty element John XML 1 07-16-2003 10:23 AM



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