Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Explanation of Code

    Hi,
    I'm new to coding so I apologize in advance if my questions are basic.

    I wrote this basic piece of code but frankly I don't understand what is actually happening

    Code:
    #(Script 1)
    
    print "What is your name?\n";
    chomp($name = <STDIN>); #waits for user input from keyboard
    print ("$name" & 'name');
    Here's the executed code
    Code:
    Process started >>>
    John Doe
    What is your name?
    Jahd<<< Process finished.
    I'm trying to learn by experimentation and I was wondering if anyone could explain why the output is Jahd. What exactly is the computer doing?
    Thanks,
    Canyon289

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by canyon289 View Post
    Code:
    print ("$name" & 'name');
    Code:
    Jahd<<< Process finished.
    What exactly is the computer doing?
    It's only doing what you asked it to do. Ever heard of "garbage in - garbage out"?

    By using the ampersand in the print function, you're telling it to print the "bitwise AND" of the input string and the string 'name'. As I understand it, when applied to strings, it operates on the character codes.
    Are you a Help Vampire?

  • #3
    Senior Coder
    Join Date
    Mar 2006
    Posts
    1,274
    Thanks
    2
    Thanked 39 Times in 38 Posts
    this line:

    print ("$name" & 'name');

    is the same as:

    print $name & 'name';

    your use of parenthesis and quotes is just confusing the issue to you. As already pointed out, & is a perl operator. If you wanted to print a string you should have done this:

    print "$name & 'name'";

    If thats not what you wanted to do then you have to let us know what your intention was.

  • #4
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for both of your replies, it clears up my question.

    To KevinADC
    I'm trying to learn by experimentation and errors. Most of the time I can see where I made a mistake and how it affected the output but in this case I had no idea what was happening. But now I know it's a bitwise operation
    Thank you so much for the explanations


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •