# Pulling specific dup. elements out of an array

**URL:** <https://www.ruby-forum.com/t/pulling-specific-dup-elements-out-of-an-array/111222>\
**Category:** Ruby\
**Created:** [August 3, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/pulling-specific-dup-elements-out-of-an-array/111222 "2007-08-03T23:42:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 3, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/pulling-specific-dup-elements-out-of-an-array/111222/1 "2007-08-03T23:42:18Z")

</div>

if i have an array full of numbers such as  
[‘234234’,‘04593’,‘4098234’,‘0’,‘0’,‘0’]

how do i remove the 0 elements without affecting the 0’s in the 04593  
and the 4098234 numbers?

my original thoughts were to use a array.reject approach but it didnt  
quite work out ☹

Thanks

-Jon

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 3, 2007, 11:50pm UTC](https://www.ruby-forum.com/t/pulling-specific-dup-elements-out-of-an-array/111222/2 "2007-08-03T23:50:23Z")

</div>

Jon H. wrote:

> how do i remove the 0 elements without affecting the 0’s in the 04593  
> and the 4098234 numbers?

array.delete(‘0’)

best,  
Dan

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 4, 2007, 1:32am UTC](https://www.ruby-forum.com/t/pulling-specific-dup-elements-out-of-an-array/111222/3 "2007-08-04T01:32:03Z")

</div>

Chris wrote:

> why didn’t reject work?
> 
> > > array = [‘234234’,‘04593’,‘4098234’,‘0’,‘0’,‘0’]  
> > > =\> [“234234”, “04593”, “4098234”, “0”, “0”, “0”]
> > 
> > > array.reject {|e| e == ‘0’ }  
> > > =\> [“234234”, “04593”, “4098234”]

well lemme further explain what im attempting to do and why i couldnt  
get reject to work right, i need to delete all the 0’s in the array then  
add up those non-zero numbers.  
with:

array.inject(0) {|num, i| num + i}/array.length/1024 #1024 =  
kilobytes convert

so if theres a way to shove reject into that then lemme know ^^

Thanks  
-Jon

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 4, 2007, 1:04am UTC](https://www.ruby-forum.com/t/pulling-specific-dup-elements-out-of-an-array/111222/4 "2007-08-04T01:04:23Z")

</div>

why didn’t reject work?

> > array = [‘234234’,‘04593’,‘4098234’,‘0’,‘0’,‘0’]  
> > =\> [“234234”, “04593”, “4098234”, “0”, “0”, “0”]
> 
> > array.reject {|e| e == ‘0’ }  
> > =\> [“234234”, “04593”, “4098234”]

(sorry if this posts twice)

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 6, 2007, 4:45pm UTC](https://www.ruby-forum.com/t/pulling-specific-dup-elements-out-of-an-array/111222/5 "2007-08-06T16:45:29Z")

</div>

2007/8/4, Jon H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> add up those non-zero numbers.  
> with:
> 
> array.inject(0) {|num, i| num + i}/array.length/1024 #1024 =  
> kilobytes convert
> 
> so if theres a way to shove reject into that then lemme know ^^

Just a small remark: you do not have numbers in your array but  
strings. I assume you want to using numeric addition and not string  
concatenation. In that case I’d do:

irb(main):001:0\> array = [‘234234’,‘04593’,‘4098234’,‘0’,‘0’,‘0’]  
=\> [“234234”, “04593”, “4098234”, “0”, “0”, “0”]  
irb(main):002:0\> nums = array.inject([]) {|a,n| a \<\< n.to\_i unless n ==  
“0”; a}  
=\> [234234, 4593, 4098234]  
irb(main):003:0\> avg = nums.inject(0) {|s,n| s+n}.to\_f / nums.size /  
1024  
=\> 1411.8037109375

Alternative if you prefer a single pass:

irb(main):018:0\> sum,count = array.inject([0,0]) {|(s,c),n| n == “0” ?  
[s,c] : [s+n.to\_i,c+1]}  
=\> [4337061, 3]  
irb(main):019:0\> avg = sum.to\_f / count / 1024  
=\> 1411.8037109375

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 6, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/pulling-specific-dup-elements-out-of-an-array/111222/6 "2007-08-06T17:08:48Z")

</div>

Jon H. wrote:

> Chris wrote:
> 
> > why didn’t reject work?
> > 
> > > > array = [‘234234’,‘04593’,‘4098234’,‘0’,‘0’,‘0’]  
> > > > =\> [“234234”, “04593”, “4098234”, “0”, “0”, “0”]
> > > 
> > > > array.reject {|e| e == ‘0’ }  
> > > > =\> [“234234”, “04593”, “4098234”]
> 
> well lemme further explain what im attempting to do and why i couldnt  
> get reject to work right, i need to delete all the 0’s in the array then  
> add up those non-zero numbers.  
> with:
> 
> array.inject(0) {|num, i| num + i}/array.length/1024 #1024 =  
> kilobytes convert
> 
> so if theres a way to shove reject into that then lemme know ^^
> 
> Thanks  
> -Jon

Why do you want to remove the 0 elements for that? They won’t influence  
the result:  
avg\_kb = array.inject(0.0) { |sum,num| sum + num.to\_f  
}/array.length/1024

If you don’t want the average without zero-size files write  
array.delete(‘0’)  
in the line before, as Daniel L. already pointed out.

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 6, 2007, 6:48pm UTC](https://www.ruby-forum.com/t/pulling-specific-dup-elements-out-of-an-array/111222/7 "2007-08-06T18:48:23Z")

</div>

Noah E. wrote:

> but rejecting the 0 elements will change the length of the array,  
> and change the resulting average. It depends on whether you want to  
> generate

From my previous post:

> If you don’t want the average without zero-size files write  
> array.delete(‘0’)  
> in the line before, as Daniel L. already pointed out.

It helps to read the whole mail ;-p

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 6, 2007, 6:30pm UTC](https://www.ruby-forum.com/t/pulling-specific-dup-elements-out-of-an-array/111222/8 "2007-08-06T18:30:48Z")

</div>

On Aug 6, 11:08 am, Stefan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Why do you want to remove the 0 elements for that? They won’t influence  
> the result:  
> avg\_kb = array.inject(0.0) { |sum,num| sum + num.to\_f
> 
> }/array.length/1024

but rejecting the 0 elements will change the length of the array,  
and change the resulting average. It depends on whether you want to  
generate  
the average across all values or the average across all non-zero  
values.

with\_zeroes = [0, 0, 0, 1.0]  
without\_zeroes = with\_zeroes.reject { |x| x == 0 } #=\> [1.0]

with\_zeroes.inject { |a,b| a + b } / with\_zeroes.length #=\> 0.25  
without\_zeroes.inject { |a,b| a + b } / without\_zeroes.length #=\> 1.0
