Sunday, January 25, 2015

Tutorial 9 : Operator Example

In this tutorial, we will learn all kind of kind of the operators which is exists in Perl language. These kind of operators are powerful to us, especially for programmer. It help us to solve a lot of problems and able to perform variety kind of operation. Below is the list of important operators which are existing in Perl language:
  • Arithmetic Operators
  • Equality Operators
  • Logical Operators
  • Assignment Operators
  • Bitwise Operators
  • Logical Operators
  • Quote-like Operators
  • Miscellaneous Operators

Arithmetic Operators

The table at below shows all the arithmetic operators which we can use in Perl.

Table 9.1: Perl Arithmetic Operators




Below is the sample coding to show you how these operators is performed and the video will show what is the expected output from them.

#!/usr/bin/perl

#Declaration of all the variable
$a = 2; $b = 4;

#Print out all the variable
print "\$a = 3 and \$b = 4\n";

#Addition Operator Example
$ans = $a + $b;
print "\$a + \$b = $ans\n";

#Subtraction Operator Example
$ans = $a - $b;
print "\$a - \$b = $ans\n";

#Multiplication Operator Example
$ans = $a * $b;
print "\$a * \$b = $ans\n";

#Division Operator Example
$ans = $b / $a;
print "\$b / \$a = $ans\n";

#Modulus Operator Example
$ans = $b % 3;
print "\$b / 3 = $ans\n";

#Exponential Operator Example
$ans = $b ** $a;
print "\$b ** \$a = $ans\n";


Equality Operators

The table at below shows all the equality operators which we can use in Perl.

Table 9.2: Perl Equality Operators
Below is the sample coding to show you how these operators is performed and the video will show what is the expected output from them.

#!/usr/bin/perl

$a = 10;
$b = 20;

# == operator example
print "1. Example of == operator: (\$a = 10; \$b = 20)\n";
if($a == $b)
{
  print "\$a equal to \$b\n\n";
}
else
{
  print "\$a not euqal to \$b\n\n";
}

$b = 10;

# != operator example
print "2. Example of != opertor:(\$a = 10; \$b = 10)\n";
if($a != $b)
{
  print "\$a not equal to \$b\n\n";
}
else
{
  print "\$a equal to \$b\n\n";
}

$a = 15;
$b = 18;

# <=> operator example
print "3. Example of <=> operator: (\$a = 15; \$b = 18)\n";
if(($a <=> $b) == 1)
{
  print "\$a is larger than \$b\n\n";
}
elsif(($a <=> $b) == 0)
{
  print "\$a is equal to \$b\n\n";
}
elsif(($a <=> $b) == -1)
{
  print "\$a is smaller than \$b\n\n";
}

# > operator example
print "4. Example of > operator: (\$a = 15; \$b = 18)\n";
if($a > $b)
{
  print "\$a is greater than \$b\n\n";
}
else
{
  print "\$a is smaller than \$b\n\n";
}

# >= operator example
print "5. Example of >= operator: (\$a = 15; \$b = 18)\n";
if($a >= $b)
{
  print "\$a is greater than or equal to \$b\n\n";
}
else
{
  print "\$a is smaller than \$b\n\n";
}

$a = 18;
$b = 11;

# < operator example
print "6. Example of < operator: (\$a = 18; \$b = 11)\n";
if($a < $b)
{
  print "\$a is smaller than \$b\n\n";
}
else
{
  print "\$a is greater than \$b\n\n";
}

$a = 12;
$b = 12;

# <= operator example
print "7. Exmple of <= operator: (\$a = 12; \$b = 12)\n";
if($a <= $b)
{
  print "\$a is smaller than or equal to \$b\n\n";
}
else
{
  print "\$a is greater than \$b\n\n";
}


Assignment Operators

The table at below shows all the assignment operators which we can use in Perl.

Table 9.3: Perl Assignment Operators

Below is the sample coding to show you how these operators is performed and the video will show what is the expected output from them.

#!/usr/bin/perl

$a = 10;
$b = 2;

# = operator example
print "\$a = 10; \$b = 2\n";

# += operator example
$a += $b;
print "\$a += \$b\n";
print "\$a = $a\n";

# -= operator example
$a -= $b;
print "\$a -= \$b\n";
print "\$a = $a\n";

# *= operator example
$a *= $b;
print "\$a *= \$b\n";
print "\$a = $a\n";

# /= operator example
$a /= $b;
print "\$a /= $b\n";
print "\$a = $a\n";

# %= operator example
$a %= $b;
print "\$a %= \$b\n";
print "\$a = $a\n";

$a = 10;

# **= operator example
$a **= $b;
print "\$a **= \$b\n";
print "\$a = $a\n";


Bitwise Operators

The table at below shows all the bitwise operators which we can use in Perl.

Table 9.4: Perl Bitwise Operators

Below is the sample coding to show you how these operators is performed and the video will show what is the expected output from them.

#!/usr/bin/perl

$a = 0b10101010;
$b = 0b01010101;
print "\$a = 10101010; \$b = 01010101\n";

# & operator example
$a = $a & $b;
print "1. \$a = \$a & \$b\n";
printf "\$a = %08b\n\n", $a;

$a = 0b10101010;

# | operator example
$a = $a | $b;
print "2. \$a = \$a | \$b\n";
printf "\$a = %08b\n\n", $a;

$a = 0b10101010;

# ^ operator example
$a = $a ^ $b;
print "3. \$a = \$a ^ \$b\n";
printf "\$a = %08b\n\n", $a;

$a = 0b10101010;

# ~ operator example
$a = ~ $a;
print "4. \$a = ~ \$a\n";
printf "\$a = %08b\n\n", $a;

$a = 0b10101010;

# >> operator example
$a = $a >> 2;
print "5. \$a = \$a >> 2\n";
printf "\$a = %08b\n\n", $a;

# << operator example
$b = $b << 2;
print "6. \$b = \$b << 2\n";
printf "\$b = %08b\n\n", $b;


Logical Operators

The table at below shows all the logical operators which we can use in Perl.

Table 9.5: Perl Logical Operators

Below is the sample coding to show you how these operators is performed and the video will show what is the expected output from them.

#!/usr/bin/perl

$a = 5;
$b = 4;
print "\$a = 5; \$b = 4\n";
print "Check \$a and \$b are equal to 4?\n\n";

# && operator example
print "1. && operatr example:\n";
if(($a == 4) && ($b == 4))
{
   print "Able to meet the condition\n\n";
}
else
{
   print "Not able to meet the condition\n\n";
}

# || operator example
print "2. || operator example:\n";
if(($a == 4) || ($b == 4))
{
   print "Either one is meet the condition\n\n";
}
else
{
   print "Not able to meet the condition\n\n";
}


Miscellaneous Operators

The table at below shows all the miscellaneous operators which we can use in Perl.

Table 9.6: Perl Miscellaneous Operators

Below is the sample coding to show you how these operators is performed and the video will show what is the expected output from them.

#!/usr/bin/perl

# . operator example
$a = "Hello, ";
$b = "John\n";
$word = $a . $b;
print "$word\n";

# x operator example
$c = ($a x 3);
print "$c\n\n";

# .. operator example
@d = (1..5);
print "@d\n\n";

# ++ operator example
$e = 4;
print "Value \$e = $e\n";
$e++;
print "Now value \$e = $e\n";

# -- operator example
$e--;
print "At last value \$e = $e\n\n";



Congratulation to everyone, we are finish this tutorial. In the coming tutorial, we will learn subroutine in Perl. Be stay tuned on. :D

2 comments: