Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   convert string array to interger array in in one line (http://www.velocityreviews.com/forums/t854028-convert-string-array-to-interger-array-in-in-one-line.html)

Jags Rao 11-05-2008 01:08 PM

convert string array to interger array in in one line
 
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/.


Stefano Crocco 11-05-2008 01:14 PM

Re: convert string array to interger array in in one line
 
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



RichardOnRails 11-05-2008 09:40 PM

Re: convert string array to interger array in in one line
 
On Nov 5, 8:08*am, Jags Rao <aquaj...@yahoo.com> 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

Todd Benson 11-05-2008 10:46 PM

Re: convert string array to interger array in in one line
 
On Wed, Nov 5, 2008 at 7:08 AM, Jags Rao <aquajags@yahoo.com> 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



All times are GMT. The time now is 10:41 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.