File & split

hi everyone,
I’m very, very fresh in ruby so
I’m looking for help with a small problem. I’ve got a log file from wget
and I have to evaluate average download speed when downloading is still
going, so I’m trying to do this

File.open(name) do |file|
while line=file.gets
tab=%w(line.split)
end
end
but how can I evaluate this 8th column from wget-log each line, I’m not
even sure about this few lines above

Logan C. wrote:

this line is almost certainly wrong, what you wanted was probably
tab = line.split
to get the value of the 8th column you’d do
eighth_column = tab[7]

thx for quick answer
hehe you’re right I was trying this but eclipse with DLTK is not to good
for ruby, it’s showing wrong results I think I will use vim like for
bash scripting :slight_smile:
but I still don’t know how to put together all this numbers

On 5/1/07, Marcin K. [email protected] wrote:

hi everyone,
I’m very, very fresh in ruby so
I’m looking for help with a small problem. I’ve got a log file from wget
and I have to evaluate average download speed when downloading is still
going, so I’m trying to do this

File.open(name) do |file|
while line=file.gets
tab=%w(line.split)

this line is almost certainly wrong, what you wanted was probably
tab = line.split
to get the value of the 8th column you’d do
eighth_column = tab[7]

On 5/1/07, Marcin K. [email protected] wrote:

for ruby, it’s showing wrong results I think I will use vim like for
bash scripting :slight_smile:
but I still don’t know how to put together all this numbers

I’ m not sure what you mean by “put together all this numbers.” A good
thing to do when you are trying to write a program is to play with it
irb.
You get immediate feedback.

Marcin K. wrote:

I’ve got this few lines
happens, all I want is to add one number to another (evaluate it)

Two things:

a) It’s “+=” not “=+”
b) You probably need to convert c8 to a number before you can add it

puts sum += c8.to_i

Logan C. wrote:

but I still don’t know how to put together all this numbers

I’ m not sure what you mean by “put together all this numbers.” A good
thing to do when you are trying to write a program is to play with it
irb.
You get immediate feedback.

I’ve got this few lines
while line=file.gets
tab=line.split
c8=tab[7].to_f
end
and it works. Results of is it’s like that
22121.12
4232.1
42353.90
322.0
when I’ll add line like this “puts sum=+c8” just before end nothing
happens, all I want is to add one number to another (evaluate it)

Tim H. wrote:

a) It’s “+=” not “=+”

yep it’s working better then before :slight_smile:

b) You probably need to convert c8 to a number before you can add it
puts sum += c8.to_i

I’ve done it in this line “c8=tab[7].to_f” but I still don’t know how to
divide sum by amount of rows :slight_smile:
eeeh it’s a crazy work, and it’s a bit late :slight_smile:

Oops, forgot to include the +=

i = 0
while line=file.gets
tab=line.split
c8+=tab[7].to_f
i = i + 1
end
puts c8 / i

How about this:

i = 0
while line=file.gets
tab=line.split
c8=tab[7].to_f
i = i + 1
end
puts c8 / i

ok, guys I solved this very tough problem :slight_smile:
and this is a solution for someone who will need it for any reason, but
I have another question how can I do this code more Ruby (I mean more
solid and nice in ruby style) because it seems for me very vulgar
sum=0.0
count=0
impr_count=0
if ARGV[0]!=nil then
File.open(ARGV[0]) do |file|
while line=file.gets
if line=~(/^([0-9]+)K/&&/%/)
count+=1
end
impr_count+=1
tab=line.split
c8=tab[7].to_f
sum+=c8
end
eval=impr_count-count
puts
puts “Av. download speed: #{sum/count} KB/s”
print "I’ve read #{count} proper & "
eval>1 ? (puts “#{eval} improper lines”) : (puts “#{eval} improper
line”)
puts
end
else
puts
print “Corupted or missing file or file path!!!\n\tPlease repeat
command with correct data\n”
puts
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs