[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

file & split

kuLa mk

5/1/2007 8:44:00 PM

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

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

5 Answers

kuLa mk

5/1/2007 9:31:00 PM

0

Logan Capaldo 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 :-)
but I still don't know how to put together all this numbers

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

kuLa mk

5/1/2007 9:58:00 PM

0

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

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

Tim Hunter

5/1/2007 10:05:00 PM

0

Marcin Kulisz wrote:
> Logan Capaldo 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)
>
>
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


--
RMagick [http://rmagick.rub...]
RMagick Installation FAQ [http://rmagick.rub.../install-faq.html]


kuLa mk

5/1/2007 10:33:00 PM

0

Tim Hunter wrote:
> a) It's "+=" not "=+"

yep it's working better then before :-)

> 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 :-)
eeeh it's a crazy work, and it's a bit late :-)

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

kuLa mk

5/2/2007 7:18:00 PM

0

ok, guys I solved this very tough problem :-)
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

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