# Pulling specific dup. elements out of an array

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

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

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
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 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)

2007/8/4, Jon H. [email protected]:

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

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
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

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

On Aug 6, 11:08 am, Stefan R. [email protected] 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