Convert integer to array?

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-forum.com/.

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

Mikel

···

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]

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

irb is friendly, just play w it..

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

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

kind regards -botp

···

From: kaepora@mac.com [mailto:kaepora@mac.com]

x.scan /\d/

  robert

···

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.

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.rubypal.com for details and updates!

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

Integer to Array

x=1234
y=x.to_s.scan(\/d\)

or
y=x.to_s.split(//)

or
y=x.to_s.split('')

Array to integer

z=y.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ña, 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=1234
# How can I convert that to the follow array: x=[1, 2, 3, 4]

irb is friendly, just play w it..

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

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

kind regards -botp

DISCLAIMER

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, please notify the sender and delete all copies of this message. Persistent Systems Ltd. does not accept any liability for virus infected mails.

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.rubypal.com for details and updates!

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.rubypal.com for details and updates!

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

···

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.

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

Ah, not really. What I meant was:

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

Cheers

robert

···

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/

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

Mikel Lindsaar wrote:

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

Most of the other ways are better, but none-the-less:

x=1234
array =

results = x.to_s.split('')
results.each{|x| array << x.to_i}

array #=> [1,2,3,4]

Regards,

- Mac

···

On Mon, May 12, 2008 at 6:36 PM, Nadim Kobeissi <kaepora@mac.com> wrote:

--
Posted via http://www.ruby-forum.com/\.

def int_split(x)
   return [0] if x.zero?
   r =
   while x > 0
     x, b = x.divmod 10
     r.unshift b
   end
   r
end

You don't need y since Fixnums are immediate. In fact, x is only a label, doing y=x just adds a new label to the object referred to by x. (Go ahead, try it with a Bignum.)

It still doesn't work for negative numbers, but since that behavior hasn't been defined, it's left as an exercise for the OP.

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On May 13, 2008, at 7:26 AM, Robert Klemme wrote:

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

>
> > 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
>

def int_split(x)
  return [0] if x.zero?
  r =

  while x > 0
    x, b = x.divmod 10
    r.unshift b
  end
  r
end

You don't need y since Fixnums are immediate.

The reasoning is wrong but comes to the right conclusion: if you want
to retain the original value of x then it does not matter whether
values are mutable or not. It is sufficient to assign to x to loose
the original value.

In my code I don't need y because x is a method parameter. My remark
was a reaction to Sebastian's comment and piece of code which modified
x.

It still doesn't work for negative numbers, but since that behavior hasn't
been defined, it's left as an exercise for the OP.

Exactly. :slight_smile:

Kind regards

robert

···

2008/5/13 Rob Biedenharn <Rob@agileconsultingllc.com>:

On May 13, 2008, at 7:26 AM, Robert Klemme wrote:
> 2008/5/12 Sebastian Hungerecker <sepp2k@googlemail.com>:

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

Hi,

Rob Biedenharn wrote:

def int_split(x)
   return [0] if x.zero?
   r =
   while x > 0
     x, b = x.divmod 10
     r.unshift b
   end
   r
end

Here is a little short version:

def int_split(x)
  r=;r[0,1]=*r[0].divmod(10)while r[0]>9;r
end

Regards,

Park Heesob

···

--
Posted via http://www.ruby-forum.com/\.

Hi --

···

On Tue, 13 May 2008, Robert Klemme wrote:

2008/5/13 Rob Biedenharn <Rob@agileconsultingllc.com>:

On May 13, 2008, at 7:26 AM, Robert Klemme wrote:

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

def int_split(x)
  return [0] if x.zero?
  r =

  while x > 0
    x, b = x.divmod 10
    r.unshift b
  end
  r
end

You don't need y since Fixnums are immediate.

The reasoning is wrong but comes to the right conclusion: if you want
to retain the original value of x then it does not matter whether
values are mutable or not. It is sufficient to assign to x to loose
the original value.

In my code I don't need y because x is a method parameter. My remark
was a reaction to Sebastian's comment and piece of code which modified
x.

I still like scanf :slight_smile:

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.rubypal.com for details and updates!

# I still like scanf :slight_smile:

indeed.

i also like the scan+map since i don't need the formatting (and the require :wink:

001:0> "1234".scan(/\d/).map(&:to_i)
=> [1, 2, 3, 4]

kind regards -botp

···

From: David A. Black [mailto:dblack@rubypal.com]

# scan+map since i don't need the formatting
# (and the require :wink:
# 001:0> "1234".scan(/\d/).map(&:to_i)
# => [1, 2, 3, 4]

arggh, 1.9 is getting better,

002:0> "1234".each_char.map(&:to_i)
=> [1, 2, 3, 4]

very readable, imho

kind regards -botp

···

From: Peña, Botp [mailto:botp@delmonte-phil.com]