How to remove empty element in an array

Hi all,

I have an array of [1,2,’’] I want change it to [1,2]. I check the
document about Array but I cant’t find a way to remove the empty
element. Any comments?

Thanks,

Li

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 cant’t find a way to remove the empty
element. Any comments?

http://ruby-doc.org/core/classes/Array.html#M000405

You should really get hold of your copy of the Pickaxe.

Regards

robert

On 10/23/06, Li Chen [email protected] 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 cant’t find a way to remove the empty
element. Any comments?

It’s “[1, 2, ]”? If so, then it’s not an empty element.
If you have [1, 2, nil] then you can just use Array#compact or
Array#compact!. Otherwise, look at #delete_if.

I put you code into a file called x and ran it

$ ruby x 9989 9991 9990 9989

39959[~]$

Seems to be working here, cant think why it shouldn’t

Peter H. wrote in post #1031329:

I put you code into a file called x and ran it

$ ruby x 9989 9991 9990 9989

39959[~]$

Seems to be working here, cant think why it shouldn’t

Hi,
I am atatchning my test files here, can you please help me .
It is not working for some how…

In unix I have the data but when I pass to ruby it is returning two
values as sum instead of one value.

Hi,

Can you please help me on this issue, I have seen your mail id from ruby
forum.

I am using ruby program for summing up all the numbers from array.
Unfortunately it is returning two values instead of one value.??
Can you please help me on this…

I appreciate your help on this…:slight_smile:

Input to array:
9989
9991
19980 -> it summing above two numbers
9990
9989
19979 -> it is summing these two numbers

all 4 numbers shoud add and retunr one value but , my ruby program
returning two values saparately(19980 ,19979 )

sum.rb code


module Enumerable

sum of an array of numbers

def sum
return self.inject(0){|acc,i|acc +i}
end
end # module Enumerable
numbers = []
print “\n”
ARGV.each { |num|
i = Float(num)
numbers << i
}
#n=numbers.sum.to_i
#print n.to_s.gsub(/(\d)(?=\d{3}+(?:.|$))(\d{3}…*)?/,’\1,\2’)
print numbers.sum.to_i
#print “\n”

—Delete ReplyReply ForwardMove

It is working only for 253 array elemnets, how to increase teh array
size.?

On Fri, Nov 11, 2011 at 1:57 AM, Konda R. [email protected] wrote:

It is working only for 253 array elemnets, how to increase teh array
size.?

What is “it”? Please do not hijack old threads.

Kind regards

robert

It is working only for 253 array elemnets, how to increase
teh array size.?

You should pastie your code somewhere.

Example:

http://pastie.org/

It’s hard to help without seeing the code. Also you should start a new
thread, right now this thread is muddled with another thread …

-----Messaggio originale-----
Da: Marc H. [mailto:[email protected]]
Inviato: venerd 11 novembre 2011 18:42
A: ruby-talk ML
Oggetto: Re: How to remove empty element in an array

It is working only for 253 array elemnets, how to increase teh array
size.?

You should pastie your code somewhere.

Example:

http://pastie.org/

It’s hard to help without seeing the code. Also you should start a new
thread, right now this thread is muddled with another thread …


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


Caselle da 1GB, trasmetti allegati fino a 3GB e in piu’ IMAP, POP3 e
SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f

Sponsor:
Conto Arancio al 4,20%. Zero spese e massima liberta’, aprilo in due
minuti!
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid922&d)-12

-----Messaggio originale-----
Da: Konda R. [mailto:[email protected]]
Inviato: gioved 10 novembre 2011 23:16
A: ruby-talk ML
Oggetto: Re: sum returning two values

Peter H. wrote in post #1031329:

I put you code into a file called x and ran it

$ ruby x 9989 9991 9990 9989

39959[~]$

Seems to be working here, cant think why it shouldn’t

Hi,
I am atatchning my test files here, can you please help me .
It is not working for some how…

In unix I have the data but when I pass to ruby it is returning two
values
as sum instead of one value.

Attachments:
http://www.ruby-forum.com/attachment/6738/stats.sum.rb
http://www.ruby-forum.com/attachment/6739/test2011-11-04
http://www.ruby-forum.com/attachment/6740/test.sh


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


Caselle da 1GB, trasmetti allegati fino a 3GB e in piu’ IMAP, POP3 e
SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f

Sponsor:
Riccione Hotel 3 stelle in centro: Pacchetto Capodanno mezza pensione,
animazione bimbi, zona relax, parcheggio. Scopri l’offerta solo per
oggi…
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid982&d)-12

-----Messaggio originale-----
Da: Konda R. [mailto:[email protected]]
Inviato: gioved 10 novembre 2011 21:36
A: ruby-talk ML
Oggetto: sum returning two values

Hi,

Can you please help me on this issue, I have seen your mail id from ruby
forum.

I am using ruby program for summing up all the numbers from array.
Unfortunately it is returning two values instead of one value.??
Can you please help me on this…

I appreciate your help on this…:slight_smile:

Input to array:
9989
9991
19980 → it summing above two numbers
9990
9989
19979 → it is summing these two numbers

all 4 numbers shoud add and retunr one value but , my ruby program
returning
two values saparately(19980 ,19979 )

sum.rb code


module Enumerable

sum of an array of numbers

def sum
return self.inject(0){|acc,i|acc +i}
end
end # module Enumerable
numbers = []
print “\n”
ARGV.each { |num|
i = Float(num)
numbers << i
}
#n=numbers.sum.to_i
#print n.to_s.gsub(/(\d)(?=\d{3}+(?:.|$))(\d{3}..*)?/,‘\1,\2’)
print numbers.sum.to_i
#print “\n”

—Delete ReplyReply ForwardMove


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


Caselle da 1GB, trasmetti allegati fino a 3GB e in piu’ IMAP, POP3 e
SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f

Sponsor:
Capodanno al parco Oltremare Riccione: Pacchetto hotel 3 stelle in
centro + ingresso al parco.

-----Messaggio originale-----
Da: Luca (Email) [mailto:[email protected]]
Inviato: gioved 29 dicembre 2011 07:56
A: ruby-talk ML
Oggetto: I: sum returning two values


Caselle da 1GB, trasmetti allegati fino a 3GB e in piu’ IMAP, POP3 e
SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f

Sponsor:
Capodanno a Riccione, Pacchetto Relax: Mezza Pensione + bagno turco +
solarium + massaggio. Wifi e parcheggio gratis. 2 giorni euro 199 a
persona
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid979&d)-12