Home arrow Perl Programming arrow Page 4 - Perl: More on Data Types and Operators

Incremental/ Decremental - Perl

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.

TABLE OF CONTENTS:
  1. Perl: More on Data Types and Operators
  2. Hashes are Yummy
  3. Operating the Deep Seas
  4. Incremental/ Decremental
  5. Comparing Numbers
By: James Payne
Rating: starstarstarstarstar / 2
October 15, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement



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.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: