Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Printing strings

Reply
Thread Tools

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";

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      02-08-2007
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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      02-08-2007
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




 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      02-08-2007
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
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      02-08-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:52 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:50 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:28 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57