Author Topic: [Ruby]Script showing the same result every time  (Read 1782 times)

0 Members and 1 Guest are viewing this topic.

Offline Dark Nebulae

  • Peasant
  • *
  • Posts: 117
  • Cookies: -79
  • Unleash the Hacker within you
    • View Profile
[Ruby]Script showing the same result every time
« on: 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
Code: [Select]
print "Enter any string: "
user_input=gets.chomp
user = []
user.push(user_input.scan(/\w+/).inspect)
puts user.length
Trust is like a piece of paper.Once it is crumbled,it can never be perfect.

Offline RedBullAddicted

  • Moderator
  • Sir
  • *
  • Posts: 519
  • Cookies: 189
    • View Profile
Re: [Ruby]Script showing the same result every time
« Reply #1 on: 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:

Code: (python) [Select]
for value in user:
      print len(value)

Cheers,
RBA
Deep into that darkness peering, long I stood there, wondering, fearing, doubting, dreaming dreams no mortal ever dared to dream before. - Edgar Allan Poe

Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: [Ruby]Script showing the same result every time
« Reply #2 on: November 22, 2012, 04:11:38 am »
just append to your code:

Code: (ruby) [Select]
p user
you'll see your error.
« Last Edit: November 22, 2012, 04:11:50 am by xzid »

Offline Dark Nebulae

  • Peasant
  • *
  • Posts: 117
  • Cookies: -79
  • Unleash the Hacker within you
    • View Profile
Re: [Ruby]Script showing the same result every time
« Reply #3 on: November 22, 2012, 03:03:06 pm »
just append to your code:

Code: (ruby) [Select]
p user
you'll see your error.
This also shows the length of 1.
Trust is like a piece of paper.Once it is crumbled,it can never be perfect.

Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: [Ruby]Script showing the same result every time
« Reply #4 on: November 22, 2012, 08:37:33 pm »
This also shows the length of 1.

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

Code: (ruby) [Select]
% 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

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

Code: (ruby) [Select]
user = user_input.scan(/\w+/)

Offline Dark Nebulae

  • Peasant
  • *
  • Posts: 117
  • Cookies: -79
  • Unleash the Hacker within you
    • View Profile
Re: [Ruby]Script showing the same result every time
« Reply #5 on: November 23, 2012, 12:44:04 pm »
p user should return [["somthing", "like" "this"]], here in irb:

Code: (ruby) [Select]
% 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. ;) ;)
Trust is like a piece of paper.Once it is crumbled,it can never be perfect.