[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

pulling specific dup. elements out of an array

Michael Linfield

8/3/2007 9:42:00 PM

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
--
Posted via http://www.ruby-....

11 Answers

Daniel Lucraft

8/3/2007 9:50:00 PM

0

Jon Hawkins 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
--
Posted via http://www.ruby-....

Chris

8/3/2007 11:02:00 PM

0

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)

On Aug 3, 4:42 pm, Jon Hawkins <globyy3...@hotmail.com> wrote:
> 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
> --
> Posted viahttp://www.ruby-....


Michael Linfield

8/3/2007 11:32:00 PM

0

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

--
Posted via http://www.ruby-....

Robert Klemme

8/6/2007 2:44:00 PM

0

2007/8/4, Jon Hawkins <globyy3000@hotmail.com>:
> 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 ^^

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

Stefan Rusterholz

8/6/2007 3:09:00 PM

0

Jon Hawkins 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 Lucraft already pointed out.

Regards
Stefan
--
Posted via http://www.ruby-....

noah.easterly@gmail.com

8/6/2007 4:28:00 PM

0

On Aug 6, 11:08 am, Stefan Rusterholz <apei...@gmx.net> 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

Stefan Rusterholz

8/6/2007 4:48:00 PM

0

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

It helps to read the whole mail ;-p

Regards
Stefan
--
Posted via http://www.ruby-....

The Cynic

3/13/2009 6:57:00 AM

0


"Frankie" <leeahkwee@yahoo.com> wrote in message
news:b2968204-8cf5-43cd-bd56-713e0a4ea052@d36g2000prf.googlegroups.com...
On Mar 12, 2:44 pm, "truth" <tr...@universe.com> wrote:
> Wow, this good doctor was murdered by the Singapore
> system. In his farewell email he claimed that he decided to
> end his life because the SAF decided to imprison him by
> extending his 12 years bond. There is just no fairness and
> justice in the system devised by LKY and his gangsters.
> Read about it here:
>
> http://wayangparty.c...


>***U yourself was unfairly treated,and suffers more injustices,did u
commit suicide?There are countless of people who undergone much worst
things in life but they don't commit suicide.This is a very cowardly
act and being deceived into thinking that Death ends all woes.It is
not.It is a new beginning of greater woes for him as he had already
found out in the nether world.

Are you from the "nether world" to assert that he had gone there? Or, can
you prove your idiotic thoughts?

>He would rather hurt all the people around him,be selfish and wanted
his own way of life.His foolish acts doesn't gets any condemnations
from most people,and this is not right.

Why must people condemn him? Does he owe them or you, a living? It is his
life and he has exclusive rights to do whatever he wants to do with it. Get
it. Stupid nut. I neither support nor encourage suicides. I sympathize with
those who resort to this desperate measure.

>He sets a very bad examples,and surely he is extremely stupid.

Don't make sweeping statements. Teh Cheang Wan also committed suicide. Do
you think he was stupid?

(all other rubbish deleted)













The Cynic

3/13/2009 12:54:00 PM

0


"Frankie" <leeahkwee@yahoo.com> wrote in message
news:8f809ac2-ebff-4107-af00-f6ddcdddd2fd@d36g2000prf.googlegroups.com...
On Mar 13, 2:56 pm, "The Cynic" <kosong...@nowhere.com> wrote:
> "Frankie" <leeahk...@yahoo.com> wrote in message
>
> news:b2968204-8cf5-43cd-bd56-713e0a4ea052@d36g2000prf.googlegroups.com...
> On Mar 12, 2:44 pm, "truth" <tr...@universe.com> wrote:
>
> > Wow, this good doctor was murdered by the Singapore
> > system. In his farewell email he claimed that he decided to
> > end his life because the SAF decided to imprison him by
> > extending his 12 years bond. There is just no fairness and
> > justice in the system devised by LKY and his gangsters.
> > Read about it here:
>
> >http://wayangparty.c...
> >***U yourself was unfairly treated,and suffers more injustices,did u
>
> commit suicide?There are countless of people who undergone much worst
> things in life but they don't commit suicide.This is a very cowardly
> act and being deceived into thinking that Death ends all woes.It is
> not.It is a new beginning of greater woes for him as he had already
> found out in the nether world.
>
> Are you from the "nether world" to assert that he had gone there? Or, can
> you prove your idiotic thoughts?
>
> >He would rather hurt all the people around him,be selfish and wanted
>
> his own way of life.His foolish acts doesn't gets any condemnations
> from most people,and this is not right.
>
> Why must people condemn him? Does he owe them or you, a living? It is his
> life and he has exclusive rights to do whatever he wants to do with it.
> Get
> it. Stupid nut. I neither support nor encourage suicides. I sympathize
> with
> those who resort to this desperate measure.
>
> >He sets a very bad examples,and surely he is extremely stupid.
>
> Don't make sweeping statements. Teh Cheang Wan also committed suicide. Do
> you think he was stupid?
>
> (all other rubbish deleted)
&&&
***An Idiot wrote:.. Why must people condemn him? Does he owe them or
you, a living? It is his
> life and he has exclusive rights to do whatever he wants to do with it.
> Get
> it. Stupid nut. I neither support nor encourage suicides. I sympathize
> with
> those who resort to this desperate measure.

>Comments: You said that you don't encourage suicides or support it?How
many evil insinuations you have written here,encouraging suicides,and
wrote wicked words?You are a real nut,and can you get away from all
your vain and hypocritical babbling?

Village Idiot. Liar. Can you prove what you had written? All you have to do
is just reproduce my so-called negative comments. I challenge you. You will
not. As usual after receiving my pummeling, you will put your tail in
between your legs and run away, as you always do.

You, like your good friend kewfatt, will only stir around the new or full
moon period. Do both of you share the same ward at IMH?

>It is clear from your words,you are a simpleton.You are shallow.

Are you talking with a mirror in front of you? LOL







truth

3/13/2009 2:13:00 PM

0

U don't even know who I am and yet u confidently
posted that I am unfairly treated. For ur info, I had
a good run while I was in Singapore.
I was never into politics.
When they started to change from a benevolent
dictatorship to a capitalist dictatorship, I turned against
them.

"Frankie" <leeahkwee@yahoo.com> wrote in message
news:b2968204-8cf5-43cd-bd56-713e0a4ea052@d36g2000prf.googlegroups.com...
On Mar 12, 2:44 pm, "truth" <tr...@universe.com> wrote:
> Wow, this good doctor was murdered by the Singapore
> system. In his farewell email he claimed that he decided to
> end his life because the SAF decided to imprison him by
> extending his 12 years bond. There is just no fairness and
> justice in the system devised by LKY and his gangsters.
> Read about it here:
>
> http://wayangparty.c...


***U yourself was unfairly treated,and suffers more injustices,did u
commit suicide?There are countless of people who undergone much worst
things in life but they don't commit suicide.This is a very cowardly
act and being deceived into thinking that Death ends all woes.It is
not.It is a new beginning of greater woes for him as he had already
found out in the nether world.

He would rather hurt all the people around him,be selfish and wanted
his own way of life.His foolish acts doesn't gets any condemnations
from most people,and this is not right.

He sets a very bad examples,and surely he is extremely stupid.

He claimed that he is not depressed.So how on earth could be that
stupid?In other words,he has the capability and strength to go through
the pressures.It sounds more like a spoit child and throwing
tantrums.Period.

People should despised such terrible deeds of such an idiot,and not
sympathy.