Home Perl Programming Page 2 - Perl: Number Crunching

# Smooth Operator - Perl

In this tutorial we will look at working with numbers in Perl. We'll cover the basics, such as integers and floating points, and end with the more advanced topics, like converting strings to numbers. With Perl you don't need to be a math genius. These simple tips can help you be a virtual Einstein.

By: James Payne
Rating:  / 6
August 05, 2008

SEARCH DEV SHED

TOOLS YOU CAN USE

As was the case in the Perl Text tutorials, some of this information I have skimmed over before. Feel free to skip over any subject you have already read about. Or if you want a refresher, keep reading.

You should be familiar with some of these math operators. If not well, why don't you let me handle your finances for you? And remember...-\$209 is more than \$100.

The first set of mathematical operators I will be forcing upon you are the +,-,*, %, and /. For those of you from the planet Alabama, those roughly translate to addition, subtraction, multiplication, modulo, and division. Here is some code showing each of them in use:

#!/usr/bin/perl

\$myPay=2400;

\$britneyspay=232000;

\$kfedspay=800;

\$subtract=9-2;

print "Kevin Federline makes " . \$kfedspay . " per month.";

\$combined= \$britneyspay+\$kfedspay;

print "\nHe and Britney used to make " . \$combined . " per month.";

print "\nI am underpaid...";

print "Below is some simple math:\n\n";

print "\n9+2 equals: " . \$add;

print "\n9-2 equals: " . \$subtract;

print "\n9*2 equals: " . 9*2;

print "\n9/2 equals: " . 9/2;

print "\n9%2 equals: " . 9%2;

This magnanimous code gives us the following result:

Kevin Federline makes 800 per month

He and Britney used to make 232800 per month

I am underpaid...Below is some simple math:

9+2 equals: 11

9-2 equals: 7

9*2 equals: 18

9/2 equals: 4.5

9%2 equals: 1

As you can see, being talentless pays well. You might also be staring at one of those equations and scratching your head (if you are Britney, you may have hired someone to scratch it for you; and it probably itches a lot now that you have shaved it). And that would be my little buddy the modulo (9%2 equals: 1). Modulo, for those who don't know, returns the remainder of a division. For instance, 9/2 equals 4 with a remainder of 1. So the result is...1. Magic, I know.

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