Mix the output?

D

Davy

Hi all,

I am a Perl newbie.

$cnt1 = 2;
$cnt2 = 3;

And I want to directly
Code:
print "The answer is "$cnt1+$cnt2;


The above code have compile error. How to mix the string and +/-/*/...
.... without middle parameter?

How to do it?

Best regards,
Davy
 
J

Jürgen Exner

Davy said:
I am a Perl newbie.

Yes, by now we know that
$cnt1 = 2;
$cnt2 = 3;

And I want to directly
Code:
print "The answer is "$cnt1+$cnt2;


The above code have compile error. How to mix the string and +/-/*/...
... without middle parameter?

No idea what you mean by 'without middle parameter', but to get rid of the
compiler error try
print "The answer is " . $cnt1+$cnt2;
or
print "The answer is ";
print $cnt1+$cnt2;

jue
 
A

axel

No idea what you mean by 'without middle parameter', but to get rid of the
compiler error try
print "The answer is " . $cnt1+$cnt2;

I think you mean:

print "The answer is ", $cnt1+$cnt2;

Axel
 
E

Eric J. Roode

Any particular reason you recommend printing a list of the two values
over concatenating the two values?

I always print a list instead of a concatenation -- but it's more of a
personal style quirk than a beneficial practice.

In principle, printing a list should be faster, since all perl has to do is
output the values, one at a time, rather than allocating a new scalar,
performing a concatenation operation, then outputting the resulting value.

For this reason, I always do

print $foo, "\n";
instead of
print "$foo\n";

In practice, I have found that it doesn't really matter much at all.

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
A

axel

Any particular reason you recommend printing a list of the two values
over concatenating the two values?

Yes. Certainly in this case as the concatenation version above will
not work as expected!

Axel
 
A

axel

No, I did mean concatenate.
Although I have to admit that I like the list even better.

#!/usr/local/bin/perl

use strict;
use warnings;

local $\ ="\n";

my $cnt1 = 2;
my $cnt2 = 3;

print "The answer is " . $cnt1+$cnt2;
__END__

Results:

Argument "The answer is 2" isn't numeric in addition (+) at clpm.pl line 11.
3

:(

Axel
 
P

Paul Lalli

Yes. Certainly in this case as the concatenation version above will
not work as expected!

Well holy crap. It didn't even occur to me that . has a higher
precedence than +. Thank you very much for the smack upside the head.
I clearly needed it.

(Can we tell it's the afternoon before a three day weekend?)

Paul Lalli
 
A

axel

I never gave any reasons... I suppose when dealing purely with
strings, there is little real no difference and concatenation
can look cleaner (at least in the way I lay out my code).
However concatenating variables holding numeric values just seems
strange to me and a potential source of error.

Axel
 
J

Jürgen Exner

No, I did mean concatenate.
Although I have to admit that I like the list even better.
[...]
Argument "The answer is 2" isn't numeric in addition (+) at clpm.pl
line 11. 3

Ooops. Wonder what's the reasoning about . having higher precedence than +.
Oh well, that's easy enough to fix with paranthesis.

But now I like the list much better.

jue
 
A

axel

Well holy crap. It didn't even occur to me that . has a higher
precedence than +. Thank you very much for the smack upside the head.
I clearly needed it.

Same precedence surely... but as they are both left associative the .
operation will take place first and then the attempted addition.
(Can we tell it's the afternoon before a three day weekend?)

:)

Merry Christmas to you and everyone on this group!

Axel
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,787
Messages
2,569,627
Members
45,328
Latest member
66Teonna9

Latest Threads

Top