[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Convert integer to array?

Nadim Kobeissi

5/12/2008 8:37:00 AM

Let's say I have:
x=1234
How can I convert that to the follow array:
x=[1, 2, 3, 4]

Any help would be greatly appreciated.
--
Posted via http://www.ruby-....

17 Answers

Mikel Lindsaar

5/12/2008 8:48:00 AM

0

On Mon, May 12, 2008 at 6:36 PM, Nadim Kobeissi <kaepora@mac.com> wrote:
> Let's say I have:
> x=1234
> How can I convert that to the follow array:
> x=[1, 2, 3, 4]

irb(main):001:0> x=1234
=> 1234
irb(main):002:0> x = x.to_s.split('')
=> ["1", "2", "3", "4"]

Mikel
http://lin...

Peña, Botp

5/12/2008 8:49:00 AM

0

RnJvbToga2FlcG9yYUBtYWMuY29tIFttYWlsdG86a2FlcG9yYUBtYWMuY29tXSANCiMgTGV0J3Mg
c2F5IEkgaGF2ZTogeD0xMjM0DQojIEhvdyBjYW4gSSBjb252ZXJ0IHRoYXQgdG8gdGhlIGZvbGxv
dyBhcnJheTogeD1bMSwgMiwgMywgNF0NCg0KaXJiIGlzIGZyaWVuZGx5LCBqdXN0IHBsYXkgdyBp
dC4uDQoNCiIxMjM0Ii5zcGxpdCgvLykNCiM9PiBbIjEiLCAiMiIsICIzIiwgIjQiXQ0KDQp5b3Vy
IG5leHQgdGFzayBpcyB0byBjb252ZXJ0IHRob3NlIGludG8gaW50ZWdlcnMsIHRoZW4geW91J3Jl
IGdvb2QgdG8gZ28uLg0KDQpraW5kIHJlZ2FyZHMgLWJvdHANCg==

Lokesh Agrawal

5/12/2008 9:08:00 AM

0

Integer to Array

x=3D1234
y=3Dx.to_s.scan(\/d\)

or
y=3Dx.to_s.split(//)

or
y=3Dx.to_s.split('')


Array to integer

z=3Dy.to_s.to_i


Thanks and Regards,

Lokesh Agrawal
Software Engineer

----------------------------------------------------------------------------
----------
"Dream is not what you see in sleep, is the the thing which does not let you
sleep"
----------------------------------------------------------------------------
----------

-----Original Message-----
From: Pe=F1a, Botp [mailto:botp@delmonte-phil.com]
Sent: Monday, May 12, 2008 2:19 PM
To: ruby-talk ML
Subject: Re: Convert integer to array?

From: kaepora@mac.com [mailto:kaepora@mac.com]
# Let's say I have: x=3D1234
# How can I convert that to the follow array: x=3D[1, 2, 3, 4]

irb is friendly, just play w it..

"1234".split(//)
#=3D> ["1", "2", "3", "4"]

your next task is to convert those into integers, then you're good to go..

kind regards -botp


DISCLAIMER=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
This e-mail may contain privileged and confidential information which is the=
property of Persistent Systems Ltd. It is intended only for the use of the=
individual or entity to which it is addressed. If you are not the intended=
recipient, you are not authorized to read, retain, copy, print, distribute=
or use this message. If you have received this communication in error, plea=
se notify the sender and delete all copies of this message. Persistent Syste=
ms Ltd. does not accept any liability for virus infected mails.

Robert Klemme

5/12/2008 9:10:00 AM

0

On 12.05.2008 10:36, Nadim Kobeissi wrote:
> Let's say I have:
> x=1234
> How can I convert that to the follow array:
> x=[1, 2, 3, 4]
>
> Any help would be greatly appreciated.

x.scan /\d/

robert

David A. Black

5/12/2008 9:55:00 AM

0

Hi --

On Mon, 12 May 2008, Nadim Kobeissi wrote:

> Let's say I have:
> x=1234
> How can I convert that to the follow array:
> x=[1, 2, 3, 4]

require 'scanf'
"1234".scanf("%1d" * 4)
# => [1, 2, 3, 4]


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!

David A. Black

5/12/2008 9:58:00 AM

0

Hi --

On Mon, 12 May 2008, Mikel Lindsaar wrote:

> On Mon, May 12, 2008 at 6:36 PM, Nadim Kobeissi <kaepora@mac.com> wrote:
>> Let's say I have:
>> x=1234
>> How can I convert that to the follow array:
>> x=[1, 2, 3, 4]
>
> irb(main):001:0> x=1234
> => 1234
> irb(main):002:0> x = x.to_s.split('')
> => ["1", "2", "3", "4"]

That's an array of strings rather than integers, though. See my other
reply, using scanf.


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!

David A. Black

5/12/2008 9:59:00 AM

0

Hi self --

On Mon, 12 May 2008, David A. Black wrote:

> Hi --
>
> On Mon, 12 May 2008, Nadim Kobeissi wrote:
>
>> Let's say I have:
>> x=1234
>> How can I convert that to the follow array:
>> x=[1, 2, 3, 4]
>
> require 'scanf'
> "1234".scanf("%1d" * 4)
> # => [1, 2, 3, 4]

"%1d" * x.size would be better (no need to hard-code the 4).


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!

Sebastian Hungerecker

5/12/2008 10:01:00 AM

0

Nadim Kobeissi wrote:
> Let's say I have:
> x=1234
> How can I convert that to the follow array:
> x=[1, 2, 3, 4]

A solution that doesn't use strings:

result_array = []
while x > 0
result_array.unshift x % 10
x /= 10
end
result_array

This will "destroy" x, though.

HTH,
Sebastian
--
NP: Depeche Mode - It's No Good
Jabber: sepp2k@jabber.org
ICQ: 205544826

Robert Klemme

5/13/2008 11:26:00 AM

0

2008/5/12 Sebastian Hungerecker <sepp2k@googlemail.com>:
> Nadim Kobeissi wrote:
> > Let's say I have:
> > x=1234
> > How can I convert that to the follow array:
> > x=[1, 2, 3, 4]
>
> A solution that doesn't use strings:
>
> result_array = []
> while x > 0
> result_array.unshift x % 10
> x /= 10
> end
> result_array
>
> This will "destroy" x, though.

Well, that's easily fixed: just work with another variable. You can
also combine division and mod:

def int_split(x)
r = []
y = x
while y > 0
y, b = y.divmod 10
r.unshift b
end
r
end

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

Robert Klemme

5/13/2008 12:09:00 PM

0

2008/5/12 Robert Klemme <shortcutter@googlemail.com>:
> On 12.05.2008 10:36, Nadim Kobeissi wrote:
>
> > Let's say I have:
> > x=1234
> > How can I convert that to the follow array:
> > x=[1, 2, 3, 4]
> >
> > Any help would be greatly appreciated.
> >
>
> x.scan /\d/

Ah, not really. What I meant was:

x.to_s.scan(/\d/).map {|i| i.to_i}

Cheers

robert

--
use.inject do |as, often| as.you_can - without end