Forum: Ruby How to Convert String to Array

613c5f4f6e55408926470d13ece615e2?d=identicon&s=25 Shengzhi Li (sphoenixee)
on 2007-03-23 23:36
Hey everyone,

Newb question: I want to read the user input an array of numbers. Is
this possible? If not, then I want to convert the string input (from
gets) into an array.

For example:

user inputs [5,3,46,6,5]
gets.chomp converts this to a string "[5,3,46,6,5]"
I want [5,3,46,6,5] (the arrray, not the string)

Any help would be greatly appreciated.

Thanks,

~sphoenixee~
B33ea5c12d767bfd1253940a960274f5?d=identicon&s=25 Tim Hunter (timhunter)
on 2007-03-23 23:50
(Received via mailing list)
On Mar 23, 6:36 pm, Shengzhi Li <shengz...@gmail.com> wrote:
> I want [5,3,46,6,5] (the arrray, not the string)
>
> Any help would be greatly appreciated.
>
> Thanks,
>
> ~sphoenixee~
>
> --
> Posted viahttp://www.ruby-forum.com/.

irb(main):017:0> x = '[5,3,46,6,5]'
=> "[5,3,46,6,5]"
irb(main):018:0> y = x[1..-2].split(',').collect! {|n| n.to_i}
=> [5, 3, 46, 6, 5]
irb(main):019:0>

1. x[1..-2] is the string without the brackets.
2. split(',') splits the string at commas into an array of strings.
3. collect! calls .to_i on each string in the array and replaces the
string with the result of the conversion.
D14c82a09135732fa0723d1832c534a4?d=identicon&s=25 Daniel (Guest)
on 2007-03-23 23:52
(Received via mailing list)
On Sat, Mar 24, 2007 at 07:36:31AM +0900, Shengzhi Li wrote:
> I want [5,3,46,6,5] (the arrray, not the string)
a = "[5,3,46,6,5]"
a.gsub!(/[\[\]]/,'').split(/\s*,\s*/)
=> ["5", "3", "46", "6", "5"]

This removes the [] first, then splits on ,

-d
D14c82a09135732fa0723d1832c534a4?d=identicon&s=25 Daniel (Guest)
on 2007-03-24 00:04
(Received via mailing list)
On Sat, Mar 24, 2007 at 07:51:27AM +0900, Daniel wrote:
> > gets.chomp converts this to a string "[5,3,46,6,5]"
> > I want [5,3,46,6,5] (the arrray, not the string)
>
> a = "[5,3,46,6,5]"
> a.gsub!(/[\[\]]/,'').split(/\s*,\s*/)
> => ["5", "3", "46", "6", "5"]
>
> This removes the [] first, then splits on ,

missed the collect as mentioned in the other response.

>
> -d

--
"You will never know how much it has cost my generation to preserve your
freedom. I hope you will make good use of it." -- John Quincy Adams

"Yes, we did produce a near-perfect republic. But will they keep it? Or
will
they, in the enjoyment of plenty, lose the memory of freedom? Material
abundance without character is the path of destruction." -- Thomas
Jefferson
C62c63cb6aa088a5b2c01570e288943d?d=identicon&s=25 Huw Collingbourne (Guest)
on 2007-03-24 00:10
(Received via mailing list)
Or then again:

x = eval("[5,3,46,6,5]" )

x is now an array!

best wishes
Huw Collingbourne

http://www.sapphiresteel.com
Ruby Programming In Visual Studio 2005
C2253345d045285df6751f7d65ff8569?d=identicon&s=25 Paul Stickney (Guest)
on 2007-03-24 00:21
(Received via mailing list)
require 'yaml'
some_obj = YAML.load(gets.chomp) rescue nil

If your input is "[1,2,3,4]" then it will return an array with 4
elements of 1, 2, 3 and 4, respectively. Of course, you could input
any valid (or not so valid) YAML and get back a String or Hash or....
The rescue is just to ensure we get a nil instead of an error on
really bad input.

Enjoy,
Paul
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-03-24 00:41
(Received via mailing list)
On Mar 23, 4:36 pm, Shengzhi Li <shengz...@gmail.com> wrote:
> I want [5,3,46,6,5] (the arrray, not the string)
irb(main):001:0> s = "[5,3,46,6,5]"
=> "[5,3,46,6,5]"
irb(main):002:0> a = s.scan( /\d+/ )
=> ["5", "3", "46", "6", "5"]
irb(main):003:0> a.map!{ |s| s.to_i }
=> [5, 3, 46, 6, 5]
85ae830f08b5ff5e39a1cbae9e3d0205?d=identicon&s=25 Olivier Renaud (Guest)
on 2007-03-24 00:58
(Received via mailing list)
Le samedi 24 mars 2007 00:20, Paul Stickney a écrit :
> Paul
I didn't know that yaml can contain bracketed arrays like this. However,
your
code seems to work only if there are spaces along with the comas :

irb(main):012:0> YAML.load("[1, 2, 3]")
=> [1, 2, 3]

but

irb(main):013:0> YAML.load("[1,2,3]")
=> [123]
613c5f4f6e55408926470d13ece615e2?d=identicon&s=25 Shengzhi Li (sphoenixee)
on 2007-03-24 01:37
Wow, I never knew there'd be so many ways to do it! Thanks so much to
all who replied! I really appreciate it.

sphoenixee
F3121a9b0999cefc9bf2d9a68439d258?d=identicon&s=25 Brian Adkins (Guest)
on 2007-03-24 04:26
(Received via mailing list)
Huw Collingbourne wrote:
> Or then again:
>
> x = eval("[5,3,46,6,5]" )
>
> x is now an array!

Unless the user actually entered something like `rm -rf /` instead of
[5,3,46,6,5] ;)
C2253345d045285df6751f7d65ff8569?d=identicon&s=25 Paul Stickney (Guest)
on 2007-03-24 10:08
(Received via mailing list)
Thanks for the correction Olivier, you are indeed correct.
I didn't realize the whitespace made a difference :-/
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-03-26 19:38
(Received via mailing list)
On Mar 23, 2007, at 5:36 PM, Shengzhi Li wrote:

> Newb question: I want to read the user input an array of numbers. Is
> this possible?

Sure:

$ ruby -rubygems read_array.rb
Enter some numbers:
1
2
3

You entered:  [1, 2, 3]
$ cat read_array.rb
#!/usr/bin/env ruby -w

require "highline/import"

array = ask("Enter some numbers:", lambda { |n| Integer(n) rescue
n }) do |q|
   q.gather = String.new
end
puts "You entered:  #{array.inspect}"

__END__

Hope that helps.

James Edward Gray II
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-03-27 18:17
(Received via mailing list)
On 3/23/07, Huw Collingbourne <huw@delthisbitdarkneon.com> wrote:
> Or then again:
>
> x = eval("[5,3,46,6,5]" )
>
> x is now an array!

Easy, but not safe if you are accepting arbitrary input from a user.

x = eval("`rm -rf .`")

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
7a3492aa0b4d739436d404f533ddfced?d=identicon&s=25 aditya j. (aditya_j)
on 2011-07-02 20:55
z = gets
  r = z.chomp
  r.gsub! /,/,''
  puts r
  n = eval("r")
   puts n.length
   puts "hello"
   puts n[1]



when i give input the output is  partially okie but i couldn,t get right
value of n[1]  which is  2  i get  50

C:\Users\Loed Ganesha>ruby r
1,2,3,4,5
12345
5
hello
50

5,3,46

but at same time this works
  x = eval("[5,3,46,6,5,7]" )
  y = x.length
  last = y
  middle = y/2
B2105bc68775435b75d8d16e87e4618f?d=identicon&s=25 Swapnil C. (swapnil_c)
on 2011-08-02 14:06
Amazing guys, a question posted in 2007 is still helping successfully to
beginners.
Thanks to all.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.