Printing strings

Discussion in 'Perl Misc' started by worlman385@yahoo.com, Feb 8, 2007.

  1. Guest

    why the follow print statement only print the number$C

    $C = 15;

    print "Number of C is " + $C +"\n";
     
    , Feb 8, 2007
    #1
    1. Advertising

  2. Guest

    wrote:
    > why the follow print statement only print the number$C
    >
    > $C = 15;
    >
    > print "Number of C is " + $C +"\n";


    If you turned on warnings, you would know.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Feb 8, 2007
    #2
    1. Advertising

  3. wrote:
    > why the follow print statement only print the number$C
    >
    > $C = 15;
    >
    > print "Number of C is " + $C +"\n";


    What do you expect to be printed if not the value of $C?
    The numerical value of the other two summands in your addition are both 0,
    so 0 + 15 + 0 should result in 15, shouldn't it?

    jue
     
    Jürgen Exner, Feb 8, 2007
    #3
  4. writes:

    > why the follow print statement only print the number$C
    >
    > $C = 15;
    >
    > print "Number of C is " + $C +"\n";


    You're using numerical addition. The numeric values of the two strings
    are 0, so essentially what you're doing above is:

    print 0 + $C + 0;

    If you want to concatenate strings, you have to use the right operator
    for that - this isn't C++ or Java, where the "+" operator is overloaded
    to do everything except make coffee. :)

    The string concatenation operator is "." (without the quotes), so what
    you wanted to do is:

    print "Number of C is " . $C . "\n";

    Another way to do that is what Perl calls "interpolation". When you use
    a double-quoted string, you can use variables directly in the string:

    print "Number of C is $C\n";

    Compare this with the output from single-quoted form, which doesn't do
    the interpolation:

    print 'Number of C is $C\n';

    And finally, concatenating a bunch of strings together just to print the
    result isn't the most efficient way to do it. You can also pass a series
    of arguments to print(), which will print them one after another:

    print "Number of C is", $C, "\n";

    To be honest though, you'd have to print a few million strings to notice
    the difference in most circumstances.

    For details, have a look at:

    perldoc perlop

    sherm--

    --
    Web Hosting by West Virginians, for West Virginians: http://wv-www.net
    Cocoa programming in Perl: http://camelbones.sourceforge.net
     
    Sherm Pendley, Feb 8, 2007
    #4
  5. -berlin.de Guest

    Sherm Pendley <> wrote in comp.lang.perl.misc:
    > writes:
    >
    > > why the follow print statement only print the number$C
    > >
    > > $C = 15;
    > >
    > > print "Number of C is " + $C +"\n";


    [...]

    > The string concatenation operator is "." (without the quotes), so what
    > you wanted to do is:
    >
    > print "Number of C is " . $C . "\n";


    [...]

    > And finally, concatenating a bunch of strings together just to print the
    > result isn't the most efficient way to do it. You can also pass a series
    > of arguments to print(), which will print them one after another:
    >
    > print "Number of C is", $C, "\n";

    ^^
    You want a blank there.

    > To be honest though, you'd have to print a few million strings to notice
    > the difference in most circumstances.


    In my view it isn't efficiency so much as the principle to use the
    simplest possible tool. Since print() has concatenation built in,
    using the dot operator falls clearly in that category.

    Anno
     
    -berlin.de, Feb 8, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon B
    Replies:
    7
    Views:
    7,613
    =?Utf-8?B?SmFzb25DaG9p?=
    Jan 30, 2006
  2. JustSomeGuy
    Replies:
    13
    Views:
    528
    msalters
    Dec 9, 2004
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    810
    Malcolm
    Jun 24, 2006
  4. iffy agbim
    Replies:
    1
    Views:
    131
    Mark Andrews
    May 24, 2004
  5. iffy agbim
    Replies:
    0
    Views:
    133
    iffy agbim
    May 21, 2004
Loading...

Share This Page