Tuesday, January 24, 2012

Learning PERL | Day 4

Hi friends! Let’s recall what we grabbed in our yesterday’s blog session. Yesterday, I told you about operators in PERL, which was actually an overview. So, today we will learn a little more about operators.

Occasionally, we need to change the value of the variables in the program. This can be done by applying some relation e.g., if the value of some constant A is 5, but you now need it to be 10, this can be done by:

A = A + 5;

Shorthand of writing this expression is:

A + = 5;

Similarly, if any similar expression can be shorthanded. Let us take another example. 

C = C – A;

Is equivalent to writing:

C - = A;

Now, if I need to increase or decrease the value of a variable by unity, this can be done in a very handy way by either adding ‘++’ or ‘--’ after or before it. For example, if I need to increase the value of variable x by 1 in the program, then the coding will be:




Now, one question might arising in your mind is what’s the difference between “x++” and “++x”? The answer is somewhat tricky to those who are new learners of programming. Actually, “x++” means “addition is carried out after the variable is used in the specified operation” and “++x” means “addition is carried out before the variable is used in any further operation”.

If you are a new learner and facing difficulty in understanding above paragraph, just leave it for today. You will be able to grab the concept in the next blog learning sessions for sure.
Similarly, if you need to decrease the value of variable y by unity, use the code:




Let us know about some other basic operators. However, we will learn their implementation in our coming blog learning sessions.

You might me familiar with “>” and “<”, known as “greater than” and “less than” operators. In addition, there are some other logic operators viz. “>=”, known as “greater than or equal to”; “<=”, known as “less than or equal to”; “!=”, known as “not equal to”; “==”, known as “is equal to”. 

The above described logic operators are used in certain logic operations. For example, consider the following code fragment:

if ($var1 > $var2)
   print “\nFirst variable is greater than second variable.”;

The first statement of this code fragment is a logic statement to judge whether first variable that is, var1 is greater that second variable, that is, var2.  
Let us take another example to understand the operation of other logic operators.
Consider following code fragment:

if ($x != 0)
    print “\nValue of x is not zero.”;

The above code fragment is whether to check the value of x is not zero. 

If I need to check whether the value of two variables are equal, following code can be used:

if ($var_1 == $var_2)
   print “\nVariable 1 equals Variable 2.”;

Note that in the above code fragment, we used ‘==’, that is, ‘equal to’ sign two times. A common mistake can be the use of single ‘equal to’ operator. This will lead to assigning of value of $var_2 to var_1. Hence, the following coding is wrong:

If ($var_1 = $var_2)
   print “\nVariable 1 equals Variable 2.”;

So, this concludes our today’s learning session on PERL learning blog. In our next sessions, we will deal with more complex coding in PERL.
As always, if you find anything difficult or if you have any suggestion, please feel free in discussing with me.

With Warm Regards,
Yajur Kumar
(PERL Programming Expert)

No comments:

Post a Comment