CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: General Discussions: General Questions:
Perl Arithmatic

 



shafathperl2011
New User

Jun 1, 2011, 12:09 PM

Post #1 of 7 (8111 views)
Perl Arithmatic Can't Post

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

Post #2 of 7 (8110 views)
Re: [shafathperl2011] Perl Arithmatic [In reply to] Can't Post

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

Post #3 of 7 (8106 views)
Re: [miller] Perl Arithmatic [In reply to] Can't Post

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

Post #4 of 7 (8103 views)
Re: [shafathperl2011] Perl Arithmatic [In reply to] Can't Post

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

Post #5 of 7 (8099 views)
Re: [miller] Perl Arithmatic [In reply to] Can't Post

...


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


shafathperl2011
New User

Jun 1, 2011, 2:52 PM

Post #6 of 7 (8094 views)
Re: [miller] Perl Arithmatic [In reply to] Can't Post

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

Post #7 of 7 (8091 views)
Re: [shafathperl2011] Perl Arithmatic [In reply to] Can't Post

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

- M

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives