Velocity Reviews > Perl > Printing strings

# Printing strings

worlman385@yahoo.com
Guest
Posts: n/a

 02-08-2007
why the follow print statement only print the number\$C

\$C = 15;

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

xhoster@gmail.com
Guest
Posts: n/a

 02-08-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

--
Usenet Newsgroup Service \$9.95/Month 30GB

Jürgen Exner
Guest
Posts: n/a

 02-08-2007
(E-Mail Removed) 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

Sherm Pendley
Guest
Posts: n/a

 02-08-2007
(E-Mail Removed) 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

Guest
Posts: n/a

 02-08-2007
Sherm Pendley <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> (E-Mail Removed) 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