Hi friends! Yesterday we learnt about different datatypes used in
PERL. The most important thing in any program is the governing logic which is
implemented by using various formulae and algorithms. To implement formulae, we
need to use certain operators with our variables so that we could get the
desired output value.
Let us introduce ourselves today with the operators used
in PERL programming.
Here are some elementary things for beginners. For simple
addition, we use ‘+’ sign, as we used in our first program. Similarly, for
subtraction, ‘-‘; for multiplication, ‘*’; for division, ‘/’. Also, ‘**’ is
used for exponentiation.
For example, if I wish to store value of variable term
xy , that is, ‘x raised to the power y’ in dependent variable
‘f’, the code fragment should be:
$f = $x ** $y;
One common mistake anyone can make is confusing with
complex formula like:
$avg = $var1 + $var2 / 2;
The above written code-fragment is intended to calculate
average value of two variables, but, is, however, wrongly coded. The reason is that precedence of ‘/’, that is, division operator over ‘+’, that is addition. To
use the code correctly, we should use braces:
$avg = ($var1 + $var2) / 2;
,which is correct now.
I found by my learning experience that cramming the precedence
sheet of operators is a really put everything in mesh. So, I recommend my
readers to better make full use of braces ‘()’. Using braces avoids unnecessary
confusion and possibility of wrong coding.
In many places, we need to store two strings stored in
separate variables to one variable. For example, $first_name contains first
name of employees in a company and $last_name contains last name of employees
and what we need is to store their full name in single variable, we use ‘.’,
that is, ‘dot’ operator. The operation is occasionally referred as concatenation.
For understanding, consider following code-fragment:
$first_name = “John”;
$last_name = “Smith”;
$full_name = $first_name . $last_name;
print $full_name;
$last_name = “Smith”;
$full_name = $first_name . $last_name;
print $full_name;
The output of above code fragment will be John Smith.
Another interesting operator is ‘x’, that is, English alphabet
small x. It is used for concatenation of string to itself in specified number
of times.
For example, consider the following code fragment:
For example, consider the following code fragment:
$string = “Hello”;
print $string x 3;
print $string x 3;
The above code will produce HelloHelloHello as output.
So, for remembering, it could be think as like multiplying the string into any
number of times. If we specify zero as the concatenation number, the string
will lead to no value or empty value, specified by “ “.
Some operators used in producing statements or strings in
the program are ‘\n’ and ‘\t’, which are known as newline and tab character
respectively. The newline character, ‘\n’, is used to produce output in new
line. ‘\t’ represents a tab.
For example, consider the following code fragment:
print “Line 1\nLine 2”;
This code will produce output as:
Line 1
Line 2
Line 2
But, what if I need to print exactly ‘Line 1\nLine 2’ on
the screen? That’s a tricky one. It can be done by using single quotes e.g., ‘string’,
instead of double quotes e.g., “string”.
Hence, the following code fragment:
Hence, the following code fragment:
print ‘Line 1\nLine 2’;
will print exactly
Line 1\nLine 2
So, this concludes your today’s learning session on PERL
blog. I hope you are still finding it relatively easier than C and C++, if you
have used them before. Otherwise, there is nothing to ponder upon.
Please feel free in e-mailing me your suggestions and
questions, if any, at yajurlive@live.com
With Warm Regards,
Yajur Kumar
(PERL Programming Expert)
Yajur Kumar
(PERL Programming Expert)
No comments:
Post a Comment