problem with reading file

Discussion in 'Ruby' started by Li Chen, Oct 20, 2006.

  1. Li Chen

    Li Chen Guest

    Hi folks,

    I read a file in the following format,extract the second column only,
    and push them into an array. My problem is that Ruby reads them as
    strings(based on array.inspect) but not as numbers so it is impossible
    for me to do the calculation. Any comments?

    Thanks,

    Li

    ###filen name: array.text


    A 1 B
    A 2 B
    A 3 B
    A 4 B
    A 5 B
    A 6 B

    ...

    ###script

    equire 'enumerator'

    array=Array.new

    File.open('array.text').each{|line| array<<line.split(/\t/)[1]}

    #puts array.inspect
    array.each_slice(3){|slice|
    average=(slice[0]+slice[1]+slice[2])/(3.0)
    puts average
    }

    ###output from screen

    C:/Ruby/self/file6.rb:8: undefined method `/' for "123":String
    (NoMethodError)
    from
    c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
    `each_slice'
    from C:/Ruby/self/file6.rb:7:in `each'
    from C:/Ruby/self/file6.rb:7:in `each_slice'
    from C:/Ruby/self/file6.rb:7



    --
    Posted via http://www.ruby-forum.com/.
    Li Chen, Oct 20, 2006
    #1
    1. Advertising

  2. On 10/20/06, Li Chen <> wrote:
    > Hi folks,
    >
    > I read a file in the following format,extract the second column only,
    > and push them into an array. My problem is that Ruby reads them as
    > strings(based on array.inspect) but not as numbers so it is impossible
    > for me to do the calculation. Any comments?
    >
    > Thanks,
    >
    > Li
    >
    > ###filen name: array.text
    >
    >
    > A 1 B
    > A 2 B
    > A 3 B
    > A 4 B
    > A 5 B
    > A 6 B
    >
    > ...
    >
    > ###script
    >
    > equire 'enumerator'
    >
    > array=Array.new
    >
    > File.open('array.text').each{|line| array<<line.split(/\t/)[1]}
    >
    > #puts array.inspect
    > array.each_slice(3){|slice|
    > average=(slice[0]+slice[1]+slice[2])/(3.0)
    > puts average
    > }
    >
    > ###output from screen
    >
    > C:/Ruby/self/file6.rb:8: undefined method `/' for "123":String
    > (NoMethodError)
    > from
    > c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
    > `each_slice'
    > from C:/Ruby/self/file6.rb:7:in `each'
    > from C:/Ruby/self/file6.rb:7:in `each_slice'
    > from C:/Ruby/self/file6.rb:7
    >


    You need to tell Ruby to coerce the strings into integers.

    array = []

    File.open('array.text') do |file|
    file.each_line do |line|
    array << line.split[1].to_i
    end
    end

    array.each_slice(3) do |triplet|
    average = (slice[0]+slice[1]+slice[2])/3.0
    puts average
    end
    Wilson Bilkovich, Oct 20, 2006
    #2
    1. Advertising

  3. Li Chen

    Li Chen Guest


    >
    > You need to tell Ruby to coerce the strings into integers.
    >
    > array = []
    >
    > File.open('array.text') do |file|
    > file.each_line do |line|
    > array << line.split[1].to_i
    > end
    > end
    >
    > array.each_slice(3) do |triplet|
    > average = (slice[0]+slice[1]+slice[2])/3.0
    > puts average
    > end



    Thank you very much,

    Li

    --
    Posted via http://www.ruby-forum.com/.
    Li Chen, Oct 20, 2006
    #3
  4. On 20.10.2006 17:25, Li Chen wrote:
    > Hi folks,
    >
    > I read a file in the following format,extract the second column only,
    > and push them into an array. My problem is that Ruby reads them as
    > strings(based on array.inspect) but not as numbers so it is impossible
    > for me to do the calculation. Any comments?
    >
    > Thanks,
    >
    > Li
    >
    > ###filen name: array.text
    >
    >
    > A 1 B
    > A 2 B
    > A 3 B
    > A 4 B
    > A 5 B
    > A 6 B
    >
    > ..
    >
    > ###script
    >
    > equire 'enumerator'
    >
    > array=Array.new
    >
    > File.open('array.text').each{|line| array<<line.split(/\t/)[1]}
    >
    > #puts array.inspect
    > array.each_slice(3){|slice|
    > average=(slice[0]+slice[1]+slice[2])/(3.0)
    > puts average
    > }
    >
    > ###output from screen
    >
    > C:/Ruby/self/file6.rb:8: undefined method `/' for "123":String
    > (NoMethodError)
    > from
    > c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
    > `each_slice'
    > from C:/Ruby/self/file6.rb:7:in `each'
    > from C:/Ruby/self/file6.rb:7:in `each_slice'
    > from C:/Ruby/self/file6.rb:7


    This can be done by an awk style 1liner :)

    18:12:44 [Temp]: cat data
    A 1 B
    A 2 B
    A 3 B
    A 4 B
    A 5 B
    A 6 B
    18:13:18 [Temp]: ruby -aF\\t -n -r enumerator -e 'BEGIN{$a=[]};
    $a<<$F[1].to_i; END{ $a.each_slice(3){|sl| p
    sl.inject(0){|a,b|a+b}/3.0}}' data
    2.0
    5.0
    18:13:34 [Temp]:

    Kind regards

    robert
    Robert Klemme, Oct 20, 2006
    #4
    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. Darrel
    Replies:
    3
    Views:
    669
    Kevin Spencer
    Nov 11, 2004
  2. Wael Soliman

    ASP.NET Reading problem (reading .xls)

    Wael Soliman, Jan 3, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,788
    =?Utf-8?B?dmluYXk=?=
    Jan 3, 2005
  3. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,404
    Scott Allen
    Jun 4, 2005
  4. Replies:
    0
    Views:
    779
  5. Karim Ali

    Reading a file and resuming reading.

    Karim Ali, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    366
    Hrvoje Niksic
    May 25, 2007
Loading...

Share This Page