file & split

Discussion in 'Ruby' started by Marcin Kulisz, May 1, 2007.

  1. 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-forum.com/.
     
    Marcin Kulisz, May 1, 2007
    #1
    1. Advertising

  2. 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-forum.com/.
     
    Marcin Kulisz, May 1, 2007
    #2
    1. Advertising

  3. 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-forum.com/.
     
    Marcin Kulisz, May 1, 2007
    #3
  4. Marcin Kulisz

    Tim Hunter Guest

    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.rubyforge.org]
    RMagick Installation FAQ [http://rmagick.rubyforge.org/install-faq.html]
     
    Tim Hunter, May 1, 2007
    #4
  5. 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-forum.com/.
     
    Marcin Kulisz, May 1, 2007
    #5
  6. 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-forum.com/.
     
    Marcin Kulisz, May 2, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    470
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    702
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    218
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    245
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    614
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page