Perl: More on Data Types and Operators

So, when we last left off, I left you lost in a tumultuous sea of data types, variables, and strings. Fortunately for you, you have the black Perl, the world’s fastest ship and a magical compass to point you in the right direction. So let’s prepare to sail, er, study, and mind the shoals.

Unfortunately, you also had that lubber Orlando Bloom blocking your path to that hottie Kiera Knightley. Not to mention a giant squid-faced pirate hot on your trail.

Well never ye fear land lubber. The good Cap’n is back and he’ll set ye sailin’ in the right direction as sure as Ben Affleck can’t act.

Yo Ho Ho and a Bottle of Data

We left off with Lists, and that’s where we will start up again. In our first example, we are going to learn how to pass a variable into a list.


#!/usr/local/bin/perl

$add_this = 1

@my_value = {$add_this ‘Dollar ‘, ‘Is ‘, ‘All ‘, ‘I ‘, ‘Own ‘};

print @my_value


  This would print out:


  1 Dollar Is All I Own

You will notice that I have a space after each value when I assign them. If I didn’t, the text would all run to together.

{mospagebreak title=Hashes are Yummy}

Writing, apparently, is hungry work. Or maybe I am just a greedy pig. For some reason, this next topic makes me think of food…hashes, hash browns…mmm, yummy. 

Anyway, as I was saying, the third data type is Hashes. Hashes are like lists, but they aren’t in order. Instead, they are indexed by a second set of scalars called keys. This might not make sense yet, but bear with me; it will.

Observe the following masterful code written by yours truly:


#!/usr/local/bin/perl

%fat_people = {‘James ‘, ’400 ‘, ‘YoMama ‘, ’900 ‘};

print %fat_people

This will output:


  James 400 YoMama 900

Please note several things. Because we used print to print the whole hash, it printed every value. Second, you will notice that the naming convention for hashes is the same as other data types, with the exception that hashes begin with a % symbol.

So this is where Hashes begin to make sense. Let’s say that I want to print only YoMama’s weight from that hash. I would do so in the following manner:


#!/usr/local/bin/perl

%fat_people = {‘James ‘, ’400 ‘, ‘YoMama ‘, ’900 ‘};

print $fat_people{‘YoMama’};

This would result in the print out:


  900

In the above code, the weight 900 is printed because we used the index key, YoMama. Instead of lining everything up in order, Hashes use the first scalar as an index key, and the second as the data, and so forth. If I wanted to print the weight of James (or 400) I would have called James instead. Simple?

Again, you will note that when I printed out the single result, I changed to the $ symbol, because again, I am printing only scalars.

To make it a smidge more clear, you could also write the above hash this way:


%fat_people = (‘James ‘ => ’400′, ‘YoMama’ => ’900′

{mospagebreak title=Operating the Deep Seas}

Argh! Thar she blows! The great white whale of the sea — or, more precisely, the treasure that every Perl diver seeks. In this case, though, it’s not something that goes into jewelry, it’s something that adds quite a bit of power to your coding. 

Operators in Perl (as in all programming languages) allow you to manipulate data. You’ve worked with operators ever since elementary school, albeit somewhat differently. 

Just as there are different data types, there are different operators for the different kinds of data. We will go through a bunch of them. First though, how would you like to see a gigantic table full of mind-numbing data? I thought you might.

Mathematical Operators


 

Symbol

What it Does

+

Adding

-

Subtracting

*

Multiplying

/

Dividing

%

Modulating

**

Exponentiating


The above operators work just as you think they would. If I wanted to figure out my weekly salary, I could do the follow code:


#!/usr/local/bin/perl


$hours = 40;

$wage = 20;

$total_salary = $hours * $wage;

print $total_salary

That would give me the following result:

  800

Operators that Assign Values


 

 

Symbol

What it Does

=

Assign Normally

+=

Assign and Add

-=

Assign and Subtract

*=

Assign and Multiply

/=

Assign and Divide

%=

Assign and Modulate

**=

Assign and Exponentiate

In the previous tutorial we used the = symbol to assign a value to a variable. We can use it for other things as well. For instance, let’s say you are the boss’s son and have just received your first paycheck. It isn’t enough to buy that Porsche you desperately need to compensate for certain characteristics you lack. Well, you can always change it like this:


$pay_rate = 20;

$pay_rate+=80;

print $pay_rate;

The above example would print your pay rate as being: 100. Another way of looking at it is saying you had written this code instead:


$pay_rate = 20;

$pay_rate=$pay_rate + 80;

print $pay_rate;

You can do the same sort of thing with the other operators above.

{mospagebreak title=Incremental/ Decremental}



Symbol

What it Does

++

Adds 1

Subtracts 1

If you want to add or subtract 1 from your value, you can use the incremental/decremental operators. Observe the code below:


$pay_rate = 20;

$pay_rate++;

This would set your pay rate to 21. If you had used — it would have lowered it to 19. Another thing to consider is the placement of your ++ symbol. Since it is after the variable, you use it differently from the way you’d use it if it was before the variable. Look at these two examples:


$pay_rate = 20;

$total_pay_rate = ++$pay_rate + 80;

The above code would result in the pay rate being: 101. This is because the operator adds to the $pay_rate variable prior to the calculation. If we wrote it this way:


$pay_rate = 20;

$total_pay_rate = $pay_rate++ + 80;

The result would be 100 because we increment the value of $pay_rate after the calculation. If you printed the $pay_rate and $total_pay_rate your results would be:

  $total_pay_rate: 100

  $pay_rate: 21

Adding a String


 

Symbol

What it Does

.

Concatenate a string

.=

Concatenate and Assign

If we want to add a string to an existing variable you could so by concatenating it.


$my_sentence = ‘This ‘ . ‘is ‘ . ‘my ‘ . ‘sentence!’;

If we printed that variable, we would get this result: This is my sentence!

We could do the same thing to add variables to each other:


$first_name = ‘James ‘;

$last_name = ‘Payne’;

$full_name = $first_name . $last_name;

The $full_name variable would now contain the data: James Payne.

Comparing Strings


 

Symbol

What it Does

eq

Equal to

ne

Not Equal to

gt

Greater than

lt

Less than

ge

Equal to or Greater Than

le

Equal to or Less Than

In addition to adding strings to one another, you can compare them to each other as well. The easiest of this group of operators is the eq operator. It says, literally, this string = that string. The other operators work in this manner: lowercase letters are greater than uppercase letters, and a is greater than b (b is greater than c, etc).

Here it is in code form:


$name_one = ‘James’;

$name_two = ‘Johnny’;

if ($name_one gt $name_two)

{

print “James is greater than Johnny!”;

}

That code tells the program that if James is greater than Johnny (and it is), print this sentence.

{mospagebreak title=Comparing Numbers}

 

Symbol

What it Does

==

Equal to

!=

Not Equal to

>

Greater than

<

Less than

>=

Greater than or Equal to

<=

Less than or Equal to

Comparing numbers is similar to comparing strings, except it is more obvious. 1 is greater than 2, 1 is equal to 1, 0 is less than 50, etc. I know, it’s complex, fourth grade math and whatnot.


$value_one = 3;

$value_two = 2;

if ($value_one gt $value_two)

{

print “3 is greater than 2!”;

}

Logical Operators

 

Symbol

What it Does

&&

AND

||

OR

!

NOT

Lastly, we come to the logical operators. These help set criteria for us to evaluate data with. We may want to write a program that asks if this value is greater than that value AND it is less than that value, do this. These are known as conditionals and we will discuss those in depth in the next tutorial.

That’s it for this tutorial. In our next tutorial we will be dealing with Conditionals and Loops, so be sure to drop by and check it out or else I will be jobless and living on the street. I’ll be that guy that asks for a dollar and then complains when all you give me is a dollar. How can I buy a bottle of rum for a dollar you cheapskate?

Till then…

Google+ Comments

Google+ Comments