EvilZone

Programming and Scripting => Scripting Languages => : Dark Nebulae November 21, 2012, 01:33:14 PM

: [Ruby]Script showing the same result every time
: 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
: Re: [Ruby]Script showing the same result every time
: RedBullAddicted November 21, 2012, 02:18:53 PM
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:

: (python)
for value in user:
      print len(value)

Cheers,
RBA
: Re: [Ruby]Script showing the same result every time
: xzid November 22, 2012, 04:11:38 AM
just append to your code:

: (ruby)
p user
you'll see your error.
: Re: [Ruby]Script showing the same result every time
: Dark Nebulae November 22, 2012, 03:03:06 PM
just append to your code:

: (ruby)
p user
you'll see your error.
This also shows the length of 1.
: Re: [Ruby]Script showing the same result every time
: xzid November 22, 2012, 08:37:33 PM
This also shows the length of 1.

p user should return [["somthing", "like" "this"]], here in irb:

: (ruby)
% 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

: (ruby)
user = []
user.push(user_input.scan(/\w+/).inspect)
to

: (ruby)
user = user_input.scan(/\w+/)
: Re: [Ruby]Script showing the same result every time
: Dark Nebulae November 23, 2012, 12:44:04 PM
p user should return [["somthing", "like" "this"]], here in irb:

: (ruby)
% 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. ;) ;)