EvilZone
Programming and Scripting => Scripting Languages => : Dark Nebulae November 21, 2012, 01:33:14 PM
-
I was trying to make a script for the hacking challenge1. When I was at the basic level that is i want the user to enter a statement and then I can store it as array but thee problem is that how long I inputs a string, the size of the array is always 1 which should not.Can anyone point a mistake in the script that I can't see? If you are able to find the mistake then plz post, I will be very grateful.The code is
print "Enter any string: "
user_input=gets.chomp
user = []
user.push(user_input.scan(/\w+/).inspect)
puts user.length
-
Hi,
I don't know much about ruby and the ruby syntax but I think the problem is that you are asking how long the array is.
As you only have one entry in your array the length is always one. Try to loop through your array and get the length of each string withing the array. With python it would look like that:
for value in user:
print len(value)
Cheers,
RBA
-
just append to your code:
p user
you'll see your error.
-
just append to your code:
p user
you'll see your error.
This also shows the length of 1.
-
This also shows the length of 1.
p user should return [["somthing", "like" "this"]], here in irb:
% irb
irb(main):001:0> user_input = "hello world, how are you doing?"
=> "hello world, how are you doing?"
irb(main):002:0> user = []
=> []
irb(main):003:0> tmp = user_input.scan(/\w+/)
=> ["hello", "world", "how", "are", "you", "doing"]
irb(main):004:0> user.push(tmp)
=> [["hello", "world", "how", "are", "you", "doing"]]
irb(main):005:0> # array inside array, so has length = 1
irb(main):006:0* user.length
=> 1
irb(main):007:0> tmp.length
=> 6
irb(main):008:0> # if you still don't understand the difference between the 2
irb(main):009:0* # try pushing some more values to user
irb(main):010:0* user.push("hello")
=> [["hello", "world", "how", "are", "you", "doing"], "hello"]
irb(main):011:0> user.length
=> 2
irb(main):012:0> user.push( %w(an another array) )
=> [["hello", "world", "how", "are", "you", "doing"], "hello", ["an", "another", "array"]]
irb(main):013:0> user.length
=> 3
in short: don't push onto the array, just assign
user = []
user.push(user_input.scan(/\w+/).inspect)
to
user = user_input.scan(/\w+/)
-
p user should return [["somthing", "like" "this"]], here in irb:
% irb
irb(main):001:0> user_input = "hello world, how are you doing?"
=> "hello world, how are you doing?"
irb(main):002:0> user = []
=> []
irb(main):003:0> tmp = user_input.scan(/\w+/)
=> ["hello", "world", "how", "are", "you", "doing"]
irb(main):004:0> user.push(tmp)
=> [["hello", "world", "how", "are", "you", "doing"]]
irb(main):005:0> # array inside array, so has length = 1
irb(main):006:0* user.length
=> 1
irb(main):007:0> tmp.length
=> 6
irb(main):008:0> # if you still don't understand the difference between the 2
irb(main):009:0* # try pushing some more values to user
irb(main):010:0* user.push("hello")
=> [["hello", "world", "how", "are", "you", "doing"], "hello"]
irb(main):011:0> user.length
=> 2
irb(main):012:0> user.push( %w(an another array) )
=> [["hello", "world", "how", "are", "you", "doing"], "hello", ["an", "another", "array"]]
irb(main):013:0> user.length
=> 3
in short: don't push onto the array, just assign
Thanks, I understood the difference between the two. ;) ;)