pulling specific dup. elements out of an array

J

Jon Hawkins

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
 
C

Chris

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)

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
 
J

Jon Hawkins

Chris said:
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
 
R

Robert Klemme

2007/8/4 said:
Chris said:
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 ^^

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
 
S

Stefan Rusterholz

Jon said:
Chris said:
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 Lucraft already pointed out.

Regards
Stefan
 
N

Noah Easterly

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
 
S

Stefan Rusterholz

Noah said:
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 Lucraft already pointed out.

It helps to read the whole mail ;-p

Regards
Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top