convert string array to interger array in in one line

Discussion in 'Ruby' started by Jags Rao, Nov 5, 2008.

  1. Jags Rao

    Jags Rao Guest

    hi guys

    i have a array like this for e.g

    [["0", "0", "0", "0", "0", "3", "3"],
    ["0", "0", "0", "0", "0", "3", "3"],
    ["0", "0", "0", "0", "0", "3", "3"],
    ["0", "3", "0", "0", "0", "0", "3"],
    ["0", "0", "0", "0", "0", "0", "3"]]

    how wud i convert to this

    [[0, 0, 0, 0, 0, 3, 3],
    [0, 0, 0, 0, 0, 3, 3],
    [0, 0, 0, 0, 0, 3, 3],
    [0, 0, 0, 0, 0, 3, 3],
    [0, 0, 0, 0, 0, 0, 3],
    [0, 0, 0, 0, 0, 0, 3],
    [0, 3, 0, 0, 0, 0, 3],
    [0, 0, 0, 0, 0, 0, 3],

    i.e a string array to integer array in 1 line if possible

    pls help
    --
    Posted via http://www.ruby-forum.com/.
    Jags Rao, Nov 5, 2008
    #1
    1. Advertising

  2. Alle Wednesday 05 November 2008, Jags Rao ha scritto:
    > hi guys
    >
    > i have a array like this for e.g
    >
    > [["0", "0", "0", "0", "0", "3", "3"],
    > ["0", "0", "0", "0", "0", "3", "3"],
    > ["0", "0", "0", "0", "0", "3", "3"],
    > ["0", "3", "0", "0", "0", "0", "3"],
    > ["0", "0", "0", "0", "0", "0", "3"]]
    >
    > how wud i convert to this
    >
    > [[0, 0, 0, 0, 0, 3, 3],
    > [0, 0, 0, 0, 0, 3, 3],
    > [0, 0, 0, 0, 0, 3, 3],
    > [0, 0, 0, 0, 0, 3, 3],
    > [0, 0, 0, 0, 0, 0, 3],
    > [0, 0, 0, 0, 0, 0, 3],
    > [0, 3, 0, 0, 0, 0, 3],
    > [0, 0, 0, 0, 0, 0, 3],
    >
    > i.e a string array to integer array in 1 line if possible
    >
    > pls help


    a.map{|i| i.map{|s| s.to_i}}

    Stefano
    Stefano Crocco, Nov 5, 2008
    #2
    1. Advertising

  3. On Nov 5, 8:08 am, Jags Rao <> wrote:
    > hi guys
    >
    > i have a array like this for e.g
    >
    > [["0", "0", "0", "0", "0", "3", "3"],
    >  ["0", "0", "0", "0", "0", "3", "3"],
    >  ["0", "0", "0", "0", "0", "3", "3"],
    >  ["0", "3", "0", "0", "0", "0", "3"],
    >  ["0", "0", "0", "0", "0", "0", "3"]]
    >
    > how wud i convert to this
    >
    > [[0, 0, 0, 0, 0, 3, 3],
    >  [0, 0, 0, 0, 0, 3, 3],
    >  [0, 0, 0, 0, 0, 3, 3],
    >  [0, 0, 0, 0, 0, 3, 3],
    >  [0, 0, 0, 0, 0, 0, 3],
    >  [0, 0, 0, 0, 0, 0, 3],
    >  [0, 3, 0, 0, 0, 0, 3],
    >  [0, 0, 0, 0, 0, 0, 3],
    >
    > i.e a string array to integer array in 1 line if possible
    >
    > pls help
    > --
    > Posted viahttp://www.ruby-forum.com/.


    Maybe this is a little clearer:

    aStrings = [["0", "0", "0", "0", "0", "3", "3"],
    ["0", "0", "0", "0", "0", "3", "3"],
    ["0", "0", "0", "0", "0", "3", "3"],
    ["0", "3", "0", "0", "0", "0", "3"],
    ["0", "0", "0", "0", "0", "0", "3"]]

    aFixnums = aStrings.map{|i| i.map{|s| s.to_i}}
    puts aStrings[0][0].class.to_s # => String
    puts aFixnums[0][0].class.to_s # => Fixnum
    puts aFixnums[0][0].integer? # => true (So a Fixnum is_a Integer)

    Note that the last line asserts that a Fixnum object is indeed an
    Integer

    HTH,
    Richard
    RichardOnRails, Nov 5, 2008
    #3
  4. Jags Rao

    Todd Benson Guest

    On Wed, Nov 5, 2008 at 7:08 AM, Jags Rao <> wrote:
    > hi guys
    >
    > i have a array like this for e.g
    >
    > [["0", "0", "0", "0", "0", "3", "3"],
    > ["0", "0", "0", "0", "0", "3", "3"],
    > ["0", "0", "0", "0", "0", "3", "3"],
    > ["0", "3", "0", "0", "0", "0", "3"],
    > ["0", "0", "0", "0", "0", "0", "3"]]
    >
    > how wud i convert to this
    >
    > [[0, 0, 0, 0, 0, 3, 3],
    > [0, 0, 0, 0, 0, 3, 3],
    > [0, 0, 0, 0, 0, 3, 3],
    > [0, 0, 0, 0, 0, 3, 3],
    > [0, 0, 0, 0, 0, 0, 3],
    > [0, 0, 0, 0, 0, 0, 3],
    > [0, 3, 0, 0, 0, 0, 3],
    > [0, 0, 0, 0, 0, 0, 3],
    >
    > i.e a string array to integer array in 1 line if possible


    Here's a weird one for your enjoyment (arr is your array)...

    require 'matrix'; p Matrix[*arr].map {|i| i.to_i}.to_a

    ...of course, just like Stefano's solution, the object must respond to
    the #to_i method.

    You can #join it and split it up (#each_slice) again (my first cool
    solution, but looks _really_ ugly). Mapping twice is the easiest and
    is the way you probably should go.

    Todd
    Todd Benson, Nov 5, 2008
    #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. =?Utf-8?B?VGltOjouLg==?=

    Cannot be converted to 'Interger'

    =?Utf-8?B?VGltOjouLg==?=, Nov 3, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,655
    chanmmn
    Nov 3, 2004
  2. uvbaz
    Replies:
    6
    Views:
    7,504
    uvbaz
    Sep 12, 2006
  3. I_have_nothing

    Any easy to printf an interger in 9,999, 99 format?

    I_have_nothing, May 13, 2005, in forum: C Programming
    Replies:
    4
    Views:
    401
    Chris McDonald
    May 13, 2005
  4. heng
    Replies:
    3
    Views:
    334
    Steven T. Hatton
    Dec 6, 2006
  5. Tim Slattery
    Replies:
    2
    Views:
    410
    Pete C
    Jan 6, 2007
Loading...

Share This Page