|
Accumulating
Operators
Accumulating Operators usually handle the functions
that count totals or repitions of an action, etc. The most
common occurence of an accumulating operator is when you count
the records in a database. The following table outlines the
different accumulating operators used widely in perl scripts,
note:
| ++ |
(Increases)
value by 1 |
| -- |
(Decreases)
value by 1 |
The above operators
often are used to save typing the longer versions
of the same assignment which can ultimately decrease
the amount of code in your script... below is some
sample code to get your feet wet:
|
#
Instead of:
$hits=$hits+1;
# You can write this:
$hits++;
# Using these operators, you must remember that you
# could also write this:
++$hits;
# If you place the ++ before the variable, the
# variable adds one to itself before it is used or
# evaluated. In this example:
$hits=10;
$totalhits= ++$hits + 15;
# The $hits variable is incremented before it is
# used in it's calculation, so it is changed to 11 before
# 15 is added to it. $totalhits turns out to be 26 in
this
# example. If you want to increment the variable after
# it is used, you use the ++ after the variable name:
$hits=10;
$totalhits= $hits++ + 15;
# In this example, $totalhits is only 25 because $hits
is
# used before being incremented, so it stays at 10 for
# this example. If you use $hits again after this, it
will
# have a value of 11.
|
|