This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

problem in Ruby code in finding permutations

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here I have marked the code line which creates problem. Look It prints right value of str but its doesn't store the right value of str in global array $arr. Why so?

 
Sheriff
Posts: 8064
569
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please answer to the next questions, so someone could actually help you:

1. What it prints?
1.1 What you expect it to print?

2. What it stores?
2.1 What you expect it to store?

Please also give us example of input and output.
 
Abhimanyu Aryan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Welcome to the Ranch.

Please answer to the next questions, so someone could actually help you:

1. What it prints?
1.1 What you expect it to print?

2. What it stores?
2.1 What you expect it to store?

Please also give us example of input and output.



sorry I missed that part I am adding output

Output:

123
132
213
231
321
312
["123", "123", "123", "123", "123", "123"]

Expected Output:

123
132
213
231
321
312
["123", "132", "213", "231", "321", "312"]
 
Liutauras Vilda
Sheriff
Posts: 8064
569
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me try it on my machine to see what happens.
 
Liutauras Vilda
Sheriff
Posts: 8064
569
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, clear. Strings are mutable in ruby. So, once you add them all in array they all referring to the same string object. After you add last element to it, your line 13 sets string to its initial form.

Add one extra print statement after line 6 to print an array and you'll see yourself ;) How to make it work in your way? We'll let you to think about it first
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic