how to convert string into number

Discussion in 'Ruby' started by Guest, Jul 5, 2007.

  1. Guest

    Guest Guest

    Hi all,

    I read several rows of numbers and split each row into an array. I get
    an array of array. By default Ruby treat each number as a string. I want
    to convert each string into a number. The only way I can think of is to
    loop through the old array of array, change each element in each row
    using #to_f and put them back into a new array. It is not efficient.
    What other options might be better?

    Thanks,

    Li

    # file format

    1 100 33 32
    2 500 33 20

    ....

    --
    Posted via http://www.ruby-forum.com/.
     
    Guest, Jul 5, 2007
    #1
    1. Advertising

  2. Hi,

    i would do it like this:


    array = []

    yourfile.each_line{|line| array << line.split(' ').collect(|item| item.to_f)}


    Cheers
    detlef

    Am Freitag, den 06.07.2007, 07:36 +0900 schrieb Guest:
    > Hi all,
    >
    > I read several rows of numbers and split each row into an array. I get
    > an array of array. By default Ruby treat each number as a string. I want
    > to convert each string into a number. The only way I can think of is to
    > loop through the old array of array, change each element in each row
    > using #to_f and put them back into a new array. It is not efficient.
    > What other options might be better?
    >
    > Thanks,
    >
    > Li
    >
    > # file format
    >
    > 1 100 33 32
    > 2 500 33 20
    >
    > ....
    >
     
    Detlef Reichl, Jul 5, 2007
    #2
    1. Advertising

  3. Guest

    Bill Kelly Guest

    From: "Guest" <>
    >
    > I read several rows of numbers and split each row into an array. I get
    > an array of array. By default Ruby treat each number as a string. I want
    > to convert each string into a number. The only way I can think of is to
    > loop through the old array of array, change each element in each row
    > using #to_f and put them back into a new array. It is not efficient.
    > What other options might be better?
    >
    > Thanks,
    >
    > Li
    >
    > # file format
    >
    > 1 100 33 32
    > 2 500 33 20


    One way would be to use #map:

    irb(main):013:0> rows = [["1", "100", "33", "32"], ["2", "500", "33", "20"]]
    => [["1", "100", "33", "32"], ["2", "500", "33", "20"]]

    irb(main):014:0> rows.each {|row| row.map! {|c| c.to_f} }
    => [[1.0, 100.0, 33.0, 32.0], [2.0, 500.0, 33.0, 20.0]]

    You could also use #to_i if you wanted integers.


    Hope this helps,

    Bill
     
    Bill Kelly, Jul 5, 2007
    #3
  4. Guest

    Guest

    Hi --

    On Fri, 6 Jul 2007, Guest wrote:

    > Hi all,
    >
    > I read several rows of numbers and split each row into an array. I get
    > an array of array. By default Ruby treat each number as a string. I want
    > to convert each string into a number. The only way I can think of is to
    > loop through the old array of array, change each element in each row
    > using #to_f and put them back into a new array. It is not efficient.
    > What other options might be better?
    >
    > Thanks,
    >
    > Li
    >
    > # file format
    >
    > 1 100 33 32
    > 2 500 33 20


    You could do:

    require 'scanf'
    File.open("filename") do |fh|
    fh.scanf("%f%f%f%f") {|n| n }
    end

    The point of the {|n| n} is to force a capture of each set of numbers,
    using the block form of scanf. If you're not sure how many numbers
    are on each line, you'd have to do a little more work but scanf might
    still be a good option.


    David

    --
    * Books:
    RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
    RUBY FOR RAILS (http://www.manning.com/black)
    * Ruby/Rails training
    & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
     
    , Jul 6, 2007
    #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. Replies:
    5
    Views:
    549
    Pete Becker
    May 21, 2005
  2. davidj411
    Replies:
    11
    Views:
    1,328
    Kam-Hung Soh
    May 29, 2008
  3. Bill

    Convert Number into a String?

    Bill, Jul 6, 2004, in forum: ASP General
    Replies:
    5
    Views:
    120
    Evertjan.
    Jul 7, 2004
  4. FAQ server
    Replies:
    1
    Views:
    158
    Bart Van der Donck
    Aug 19, 2006
  5. FAQ server
    Replies:
    6
    Views:
    176
    Dr J R Stockton
    Nov 3, 2006
Loading...

Share This Page