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~
On Mar 23, 6:36 pm, Shengzhi Li [email protected] 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>
- x[1β¦-2] is the string without the brackets.
- split(β,β) splits the string at commas into an array of strings.
- collect! calls .to_i on each string in the array and replaces the
string with the result of the conversion.
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
Or then again:
x = eval(β[5,3,46,6,5]β )
x is now an array!
best wishes
Huw C.
http://www.sapphiresteel.com
Ruby P.ming In Visual Studio 2005
On Mar 23, 4:36 pm, Shengzhi Li [email protected] 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]
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
1 Like
Le samedi 24 mars 2007 00:20, Paul S. 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]
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
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
Thanks for the correction Olivier, you are indeed correct.
I didnβt realize the whitespace made a difference :-/
Huw C. 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]
On 3/23/07, Huw C. [email protected] 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/
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 G. II
Amazing guys, a question posted in 2007 is still helping successfully to
beginners.
Thanks to all.
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