Home: General Discussions: General Questions:
Perl Arithmatic



shafathperl2011
New User

Jun 1, 2011, 12:09 PM


Views: 14014
Perl Arithmatic

Here is the code i have

$var1=100
$var="information"

print $var1+$var2-20

----------------------------------------------
This prints me "INF".. I'm totally clueless what is happening over there.


miller
User

Jun 1, 2011, 1:01 PM


Views: 14013
Re: [shafathperl2011] Perl Arithmatic

Yes, and do you have a question?

- M


(This post was edited by miller on Jun 1, 2011, 1:01 PM)


shafathperl2011
New User

Jun 1, 2011, 2:18 PM


Views: 14009
Re: [miller] Perl Arithmatic

Here is the code i have

$var1=100
$var="information"

print $var1+$var2-20

----------------------------------------------
This prints me "INF".. I'm totally clueless what is happening over there. Can anyone help me understand why it is printing INF ? Why not INFORMATION or the value 80 ?


miller
User

Jun 1, 2011, 2:40 PM


Views: 14006
Re: [shafathperl2011] Perl Arithmatic

Add use strict; and use warnings; do the beginning of every script and declare your variables.

Also, don't forget to use a semicolon to end a statement.

That will change your code to the following:


Code
use strict; 
use warnings;

my $var1 = 100;
my $var2 = "information";

print $var1+$var2-20;


- M


(This post was edited by miller on Jun 1, 2011, 2:44 PM)


shafathperl2011
New User

Jun 1, 2011, 2:43 PM


Views: 14002
Re: [miller] Perl Arithmatic

...


(This post was edited by shafathperl2011 on Jun 1, 2011, 2:44 PM)


shafathperl2011
New User

Jun 1, 2011, 2:52 PM


Views: 13997
Re: [miller] Perl Arithmatic

I modified the code as below

Code1 :
=======

#! /usr/bin/perl

use strict;
use warnings;

my $var1=100;
my $var2="safath";

print $var1+$var2-20;
print "\n";

Output1
========
Argument "safath" isn't numeric in addition (+) at checkArithmetic.pl line 9.
80


If I change the code as,

Code 2:
======

#! /usr/bin/perl

use strict;
use warnings;

my $var1=100;
my $var2="information";

print $var1+$var2-20;
print "\n";

It prints:

Output2:
=======


Argument "information" isn't numeric in addition (+) at checkArithmetic.pl line 9.
INF

My question is , the output should be '80', but why it is printing 'INF' ?


miller
User

Jun 1, 2011, 5:17 PM


Views: 13994
Re: [shafathperl2011] Perl Arithmatic

I get 80 to both versions of the script. It shouldn't change simply because you change the string inside $var2.

- M