Perl
  Home arrow Perl arrow Page 4 - Perl: Number Crunching
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Perl: Number Crunching
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2008-08-05


    Table of Contents:
  • Perl: Number Crunching
  • Smooth Operator
  • Operator Precedence
  • The Positive and the Negative
  • Proselytizing Numbers into Holy Strings!

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Perl: Number Crunching - The Positive and the Negative
    ( Page 4 of 5 )

    To make a number a negative number, you simply add the - sign to the left of it. For example, -4 equals, well, negative 4. If we want to make a negative number a positive number, we can say -(-5). This is because two negatives make a positive. Here is how negative and positive numbers work in Perl:


    #!/usr/bin/perl

    $a = "\n";

    print -(-5);

    print $a;

    print -4 - -2;

    print $a;

    print -4 - -4;

    print $a;

    print -4 + 5;

    print $a;

    print 5 - -6;

    print $a;

    print +(+5);

    print $a;

    print 5 - 6;

    print $a;

    print -5 * 5;

    print $a;

    print 5 / -5;

    The results:

      5

      -2

      0

      1

      11

      5

      -1

      -25

      -1

    Your Assignment: Math

    In addition to assigning a value to a variable, you can also perform math at the same time. For instance, let's say that we have a variable and we want to increase its value by ten. Here is one way of doing it:

     

    #!/usr/bin/perl

    $value = 10;

    print $value . "\n";

    $value = $value * 10;

    print $value;

    This assigns the value 10 to the variable $value, prints it,  and then takes the amount in the variable and multiplies it by ten, reassigns it, and prints it once more. Resulting in:

      10

      100

    An easier way to do this is with the *= operator:


    #!/usr/bin/perl

    $value = 10;

    print $value . "\n";

    $value *= 10;

    print $value;

    Giving us the same result:

      10

      100

    It may not seem like a huge time saver, but over time it can be.

    You can do this with the other math operators as well, like so:


    #!/usr/bin/perl

    $value = 10;

    print $value . "\n";

    $value *= 10;

    print $value . "\n";

    $value +=2;

    print $value . "\n";

    $value -= 1;

    print $value . "\n";

    $value /= 2;

    print $value . "\n";

    $value **= 2;

    print $value . "\n";

    The result is:

      10

      100

      102

      101

      50.5

      2550.25



     
     
    >>> More Perl Articles          >>> More By James Payne
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT