Simple Perl Problem?

D

Davy

Hi all,

I am a Perl newbie.

$cnt1 = 2;
$cnt2 = 3;

And I want to directly print $cnt1+$cnt2. (i.e. print 5)

But it seems print "$cnt1+$cnt2"; and print '$cnt1+$cnt2'; does not
work.

How to do it?

Best regards,
Davy
 
J

John W. Krahn

Davy said:
I am a Perl newbie.

$cnt1 = 2;
$cnt2 = 3;

And I want to directly print $cnt1+$cnt2. (i.e. print 5)

But it seems print "$cnt1+$cnt2"; and print '$cnt1+$cnt2'; does not
work.

How to do it?

print $cnt1 + $cnt2;



John
 
R

raghu

If you want to print the result for example addition you should not put
in double or single quoted strings rather you can assign the value of
addition to third variable and then you can put in double strings, it
will give result, as you assigned to another variable and you can't use
single quoted string if you use it will print what is included in that
string but not value,that's the real magic of perl.
 
J

Jürgen Exner

Davy said:
$cnt1 = 2;
$cnt2 = 3;

And I want to directly print $cnt1+$cnt2. (i.e. print 5)

But it seems print "$cnt1+$cnt2";

Double quotes define a string(!) where variables are interpolated, i.e.
their names are replaced with their values.
and print '$cnt1+$cnt2'; does not work.


Single quotes define a literal string(!), i.e. variables are _not_
interpolated.
But you don't want a string in the first place, you want perl to evaluate an
expression.
How to do it?

Simple. You don't want a string, so loose those quotes and write an
expression instead:

print $cnt1 + $cnt2;

jue
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top