[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to Convert String to Array

Shengzhi Li

3/23/2007 10:37:00 PM

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~

--
Posted via http://www.ruby-....

12 Answers

Tim Hunter

3/23/2007 10:47:00 PM

0

On Mar 23, 6:36 pm, Shengzhi Li <shengz...@gmail.com> wrote:
> 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~
>
> --
> Posted viahttp://www.ruby-....

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.

Daniel

3/23/2007 10:51:00 PM

0

On Sat, Mar 24, 2007 at 07:36:31AM +0900, Shengzhi Li wrote:
> 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)

a = "[5,3,46,6,5]"
a.gsub!(/[\[\]]/,'').split(/\s*,\s*/)
=> ["5", "3", "46", "6", "5"]

This removes the [] first, then splits on ,

-d

Daniel

3/23/2007 11:04:00 PM

0


On Sat, Mar 24, 2007 at 07:51:27AM +0900, Daniel wrote:
> On Sat, Mar 24, 2007 at 07:36:31AM +0900, Shengzhi Li wrote:
> > 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)
>
> 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

Huw Collingbourne

3/23/2007 11:09:00 PM

0

Or then again:

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

x is now an array!

best wishes
Huw Collingbourne

http://www.sapphir...
Ruby Programming In Visual Studio 2005


Paul Stickney

3/23/2007 11:20:00 PM

0

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

Gavin Kistner

3/23/2007 11:36:00 PM

0

On Mar 23, 4:36 pm, Shengzhi Li <shengz...@gmail.com> wrote:
> 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)

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]

Olivier

3/23/2007 11:55:00 PM

0

Le samedi 24 mars 2007 00:20, Paul Stickney a écrit :
> 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

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]


--
Olivier Renaud

Shengzhi Li

3/24/2007 12:38:00 AM

0

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

--
Posted via http://www.ruby-....

Brian Adkins

3/24/2007 3:22:00 AM

0

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] ;)

> best wishes
> Huw Collingbourne
>
> http://www.sapphir...
> Ruby Programming In Visual Studio 2005
>
>

Paul Stickney

3/24/2007 9:07:00 AM

0

Thanks for the correction Olivier, you are indeed correct.
I didn't realize the whitespace made a difference :-/