# Perl: Number Crunching

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.

The year was…well I don’t know exactly what year it was. But there stood Pythagoras of Samos, the great Greek magician and founder of the religion, Pythagoreanism, which is almost as wacky as Scientology, except L. Ron Hubbard didn’t name it Hubbardism. You probably know this Greek bad-ass as the guy who made you fail all your math classes when you couldn’t tell your professor what the Pythagorean Theorem was.

At any rate, there he stands, and next to him is Hipassus of Metapontum, who takes a sip of his Mountainus Dewus and says, "Hey…I discovered this thing called irrational numbers." He then went on to explain them to Pyth, who, well, got really Pythed off, exclaimed he would not accept the existence of irrational numbers, and sentenced Hippasus to death by drowning.

So what’s my point in telling you all that? Numbers are best left to those that know them well, and hardly anyone knows them better than computers. Sometimes this can make working with math in programs difficult, because some are real finicky about how you do your equations. But not our boy Perl (who by the way, has a very effeminate name). Perl makes math simple. Whereas in some languages you have to differentiate between the data types of the math you are performing, in Perl there is no need. And while that might not make sense right this minute, it will very shortly.

Enough rambling. As Morpheus says to Neo in the Matrix…Damn these are some good biscuits.

These Numbers Be Keeping It Real

In the world of computers there are two types of numbers: integers and floating-point numbers (also known as real numbers, hence my witty headline). Floating point numbers are decimals, while integers are whole numbers.

In the majority of programming languages, you need to differentiate between floating point and integer numbers. Perl, however, is a math magician. When you type in the number 49, it says, "Hey…that’s an integer." If you type in 2.7, then again Perl uses its massive brain to explain to the computer that that number is a floating point.

Here is some sample code showing how to declare integer and floating point variables:

#!/usr/bin/perl

\$real=7.11;

\$regetni=8;

print "This numba be keepsin it real: " . \$real;

print "n This is an integer: " . \$regetni;

As you can see, you do so the same way. Here is the result:

This numba be keepsin it real: 7.11

This is an integer: 8

{mospagebreak title=Smooth Operator}

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:nn";

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.

{mospagebreak title=Operator Precedence}

In Perl, and I believe pretty much most of the languages out there, you give precedence to an equation by encapsulating it in parentheses. Consider the following:

5 + 9 * 2 = 28

5 + (9 * 2) = 23

By wrapping the equation 9 * 2 in parentheses, we force it to be calculated first, giving a different result. Even though you don’t deserve it, here it is in code:

#!/usr/bin/perl

\$complex = (9*2) – (2*9) + (180/10) * 10 * (2 * 5) / 100;

print \$complex;

This gives us the result:

18

Exponentially Yours

The exponential operator (**) allows you to get the power of a number. For instance, 5**100 is 5 to the hundredth power, or 7.88860905221012e+069. Or simply put, some ridiculously crazy number. Something simpler to comprehend might be 3**3, which is 3 to the third power or 3 * 3 * 3 (which of course equals 27). Here it is in code:

#!/usr/bin/perl

\$power = 0**10;

\$powera = 1**10;

\$powerb = 2**10;

\$powerc = 3**10;

\$powerd = 4**10;

print \$power . "t" ;

print \$powera . "t";

print \$powerb. "t" ;

print \$powerc. "t" ;

print \$powerd. "t" ;

And the result is…

0 1 1024 59049 1048576

{mospagebreak title=The Positive and the Negative}

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

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

{mospagebreak title=Proselytizing Numbers into Holy Strings!}

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…