Friday, March 9, 2012

Updating Very Soon

We are undergoing a self-manipulation process on this learning blog. We will be back within next 8 hours!

Monday, February 13, 2012

Learning PERL | Day 7


Today we will try to grab some facts about useful built-in functions used in PERL. Actually, built-in functions provide us useful means of manipulating operations in PERL without writing some complex codes.

One such instance of built-in function is square-root function. It is denoted in PERL by using keyword “sqrt”. To understand its use, let us have a look at the program code fragment given below:

$NUM = 16;
print sqrt $NUM;

It will print the digit 4 on the screen. Another ‘good-looking’ way of writing this code fragment is to include the $NUM in braces. Although, it is not required in PERL, yet, it increases the readability of the program to you. 

Another built-in function of interest is “int”. It is used to print only the part of number before the decimal point. For example, if used with 56.678, it will return 56 to the program. Let us understand its concept through a code fragment:

$NUM = 56.678;
print int ($NUM);

In a similar manner, the function ‘print’ prints everything given to it on screen, as we unknowingly used it in our almost all programs.

There are a series of arithmetic in-built functions in PERL, which we deal in our coming lectures. At this point, I would like to introduce some functions which are used to be operate on strings. One such function is “length”. It is used to give the number of characters used in the string. Let us take an example.

$Test_String = “This is a test string.”;
print length($Test_String);

Provided the spaces are also count in characters, the output of the above code-fragment is 22. 

Another in-built string functions are “lc” and “uc”. These are used for converting the string to all-lower case and all-upper case respectively, irrespective in which case they are initially in.
Let us take an example to understand it:

print uc(“convert me to upper case”);

The above code fragment will print the string in all upper-case as: CONVERT ME TO UPPER CASE. Similar method applies with the built-in function “lc”.

In addition to these basic string functions, there are a variety of other string functions used in PERL, which we deal in our coming blog learning sessions. In addition to the functions which takes arguments to perform an operation, there are functions, which need no argument to operate. Such a function, which is usually prove important in game programming is “rand” function. 

“rand” function is used to print a random number between 0 and 9. Let us understand it with the help of an example. 

print rand();

It will print any random number between 0 and 9, say, it be 7.0304934458. However, sometimes, we need random numbers between certain upper limit, say between 0 and 7.009. In this condition, the upper limit can be treated as the argument of the “rand” function. Taking an example:

print rand(7.009);

It will print a random number between 0 and 7.009, say it to be, 5.373643441, but it will never be 7.9344344351.

PERL has many in-built functions which prove useful in various operations. We will learn about some important of them in our coming blog learning sessions.

Hope you are finding these blog learning sessions useful and enjoyable!

With Warm Regards,
Yajur Kumar
(PERL Programming Expert)

Monday, January 30, 2012

Learning PERL | Day 6


Hi friends! In our last blog learning session, we grabbed some knowledge about the flow of control in PERL programming by using ‘if-else’ statements. Let us understand today these ‘if-else’ statements by means of some examples.

Consider you are asked to build a program which can classify the customers in a bank as premium, important and regular customer on the basis of the balance they have in their bank accounts. Classification is to be made like this: 

“those customers with amount deposited over $1,000,000 are to be treated as premium; those having amount deposited between $500,000 and $1,000,000 are to be treated as important customer; and finally, those left, i.e., customers with deposits below $500,000 are to be treated as regular customers.”

This problem can be converted into a PERL program which can be used to distinguish the various classes of customers. Let us see how it is done:

if ($balance > 1000000)
{
  print “\nPremium Customer.”;
}
elsif ($balance > 500000)
{
  print “\nImportant Customer.”;
}
else
{
  Print “\nRegular Customer.”;
}


In the program illustrated above, the flow is designed in such a way that there is the need of only one condition to be specified in the body of execution. ‘Body of Execution’ is the block below each condition, it is included in the curly-brackets ( { <body> } ).

One more thing to notice here is that we do not put terminator (;) after the if or elsif or else statement. It is because the statement as a whole does not terminates at the same line, if found correct, it’s body will be executed, which is in the next line starting with ‘{‘ and ending with ‘}’. It is a common mistake to put terminator after the flow statement.

One more that I want to discuss today is the use of comments in our programs. Suppose you are working on a big-big and complex program which uses various formulae and statements. It is a usual thing that you may not be able to grasp your own coding if viewed later. So, to keep our program meaningful to us and to other programmers, we use some statements which help us in grabbing the idea about our program. These statements are completely ignored by the PERL interpreter.

In PERL, comments begin with ‘#’ symbol. In other words, if you put ‘#’ in a line, all the characters after become invisible to interpreter. Let us re-write the above program using comments to make it more illustrative.

if ($balance > 1000000)          # first condition
{
  print “\nPremium Customer.”;   #  Body of first condition
}
elsif ($balance > 500000)        # second condition
{
  print “\nImportant Customer.”; #  Body of second condition
}
Else                             #  Final condition
{
  Print “\nRegular Customer.”;  #  Body of final condition
}


This concludes today’s learning session on PERL learning blog. Hope you are finding these learning sessions interesting and easy to understand.


With Warm Regards,
Yajur Kumar
(PERL Programming Expert)

Thursday, January 26, 2012

Learning PERL | Day 5


Hi friends! Till now, I think we have grabbed sufficient to start out study of flow of control in PERL programming. By flow of control, I mean how the control of program passes from one step to another. It can be understood as the main deciding factor or program flow logic.
In our previous blog learning session, I introduced ‘if-else’ statements; however, I left their deep understanding for today. So, let us understand the functioning of these ‘if-else’ statements today.

The ‘if’ statement is used to specify a particular condition, often, a logic statement. It can be understand by following syntax:

if (<condition>)
{
   <Steps to follow>
}
Consider the following code fragment to understand this syntax:

if ($x==0)
{
    print “\nThe value of x is zero”;
}

The above code fragment is used to check whether the value of x is zero. To judge this, we first used if statement with condition “$x==0”, which means the statement that “x equals zero”. If this condition is found true, the statement body will be executed, which prints “The value of x is zero.” on the screen, otherwise, it will be skipped.

If it is needed that provided the first condition is skipped, the program will check for the second condition, ‘if-else’ or ‘else’ statement is used. The general syntax of ‘if-else’ nesting, which can be extend to the limit we need, is:

if (<condition_1>)
{
    <Steps to follow>
}
else if (<condition_2>)
{
   <Steps to follow, provided condition_1 is wrong>
}
else
{
   <Steps to follow, provided all conditions are wrong>
}

We will understand the ‘if-else’ nesting with a suitable example in our tomorrow’s learning session on this blog.
Hope you enjoyed today’s learning session on PERL.
Happy Republic Day to my readers from India.

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)


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:

x++;

or

++x;

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:

y--;

or

--y;

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)

Monday, January 23, 2012

Learning PERL | Day 3


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;

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:

$string = “Hello”;
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

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:

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)

Sunday, January 22, 2012

Learning PERL | Day 2


Welcome back friends!

Yesterday we learn about writing a simple addition program in PERL. To recall you, we coded a program for simple addition in which we used two variables, “$x” & “&y”:
 
$x = 100;
$y = $x + $x;
print $y;

I stated in my previous blog that variables are used for storing ‘varying data’ that we need or generated in the program. So, you may have noticed one thing about the variable names e.g., in “$x” and “$y”, why we are using this “$” sign before any variable? The answer is related to the identification of these variables as ‘variables’ by the PERL interpreter. The “$” sign tells the PERL interpreter that it is dealing with a variable.

A variable name, which starts from “$” sign, may contain letters(a-z, A-Z), numbers(0-9), a special character “_”, known as underscore or a mixture of these. Example of variable names are $value1, $value2, $max_value, $min_value etc. There is a limit on number of letters you can use in variable names, but, don’t worry, I’m sure you will never need to jump over it.

The variable names should be descriptive and handy to you. Such as if you are writing a program to compute EMI on loan amount, and you use variable names as “$x” and “$y”, and so on, your program may become uninformative after a few steps, that’s you waste most of your time in remembering what x stands for, what y stands for etc. I suggest using mixed type variable names; however, you can omit vowels in words. For example, a good variable for loan amount can be “$amt_loan”.

For those who have used to code in ‘C’ before, they may have familiar with the ‘Datatypes’. For beginners, datatypes simply define what type of data you are entering or what type of information you want in return from the machine. 
For example, in some cases you may need integer values like 1423, 88383 etc., and in some other cases you may need to input contact names as “John Smith”, “Yajur Kumar” etc. So, datatypes simply tells what type of data the program is dealing with.

PERL uses three types of datatypes, namely, scalars, arrays and hashes. Today we only deal with them in short and will learn about them in detail in our next learning sessions in this blog.

Scalars can have any kind of value, viz. a character, a whole number, an integer, or a string. Scalars variable names starts with “$” symbol, as we learned in our first program. Further, for now just remember that array variables initialize with “@” symbol and hash variables starts with “%” symbol.

Also, I like one thing of PERL that it does not need to declare any variable before using it like other programming languages e.g., C and C++. We can use the variables directly without bothering about their declaration in the program.

So, this concludes your second day at PERL learning blog. Tomorrow we will go through some basics in scalar data types.

Hope you find today’s blogpost interesting.

With Warm Regards,
Yajur Kumar
(PERL Programming Expert)