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