Perl
  Home arrow Perl arrow Page 5 - 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 - Proselytizing Numbers into Holy Strings!
    ( Page 5 of 5 )

    Converting numbers into strings is pretty simple in Perl. Here is an example showing just how to do so:


    #!/usr/bin/perl

    $ravioli = 150;

    $ravioli = $ravioli . " Meatballs on my plate. Yum.";

    print $ravioli;

    This will print out the following text:

      150 Meatballs on my plate.

    Pretty simple. Now let's see what happens when we add math into the mix:


    #!/usr/bin/perl

    $ravioli = 150;

    print $ravioli * 10;

    $ravioli = $ravioli . " Meatballs on my plate. Yum.\n";

    print $ravioli * 10;

    For me, this code does something very odd. At first it prints the 1500, as it should. Then, for some reason, it prints 1500 again, when it should print the 150 Meatballs sentence ten times. This is a known problem with converting numbers to strings; if you try to perform numbers on them after the conversion, it won't necessarily work. For all of you nay-sayers, here is a little more code to prove my point:


    #!/usr/bin/perl

    $ravioli = 150;

    print $ravioli * 10;

    $ravioli = $ravioli . " Meatballs on my plate. Yum.\n";

    print $ravioli * 10;

    print $ravioli;

    The result?

      15001500150 Meatballs on my plate. Yum.

    As you can see, the value was converted, but when we applied math to it, it went nuts and thought it was still a number.

    Well that's all the time we have for this article. Come back next time for more Perls of wisdom. Till then...



     
     
    >>> 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 6 Hosted by Hostway
    Stay green...Green IT