Server Side Programming Articles - Page 2071 of 2650

Perl Equality Operators

Mohd Mohtashim
Updated on 29-Nov-2019 06:12:47

490 Views

These are also called relational operators in Perl. Assume variable $a holds 10 and variable $b holds 20 then, let's check the following numeric equality operators available in Perl −Sr.No.Operator & Description1== (equal to)Checks if the value of two operands are equal or not, if yes then condition becomes true.Example− ($a == $b) is not true.2!= (not equal to)Checks if the value of two operands are equal or not, if values are not equal then the condition becomes true.Example− ($a != $b) is true.3Checks if the value of two operands are equal or not, and returns -1, 0, or 1 ... Read More

Perl Arithmetic Operators

Mohd Mohtashim
Updated on 29-Nov-2019 06:10:13

259 Views

Assume variable $a holds 10 and variable $b holds 20, then following are the Perl arithmetic operators −Sr.No.Operator & Description1+ ( Addition )Adds values on either side of the operatorExample− $a + $b will give 302- (Subtraction)Subtracts right-hand operand from the left-hand operandExample− $a - $b will give -103* (Multiplication)Multiplies values on either side of the operatorExample− $a * $b will give 2004/ (Division)Divides left-hand operand by right-hand operandExample− $b / $a will give 25% (Modulus)Divides left-hand operand by right-hand operand and returns the remainderExample− $b % $a will give 06** (Exponent)Performs exponential (power) calculation on operatorsExample− $a**$b will give ... Read More

The Infinite Loop in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 06:06:58

1K+ Views

A loop becomes an infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the loop are required, in Perl, you can make an endless loop by leaving the conditional expression empty.#!/usr/local/bin/perl for( ; ; ) {    printf "This loop will run forever."; }You can terminate the above infinite loop by pressing the Ctrl + C keys.When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but as a programmer more commonly use the ... Read More

Loop Control Statements in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 06:05:21

2K+ Views

Loop control statements change the execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.Perl supports the following control statements. Click the following links to check their detail.Sr.No.Control Statement & Description1next statementIt causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.2last statementTerminates the loop statement and transfers execution to the statement immediately following the loop.3continue statementA continue BLOCK, it is always executed just before the conditional is about to be evaluated again.4redo statementThe redo command restarts the loop block without ... Read More

The ? : Operator in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 06:01:59

497 Views

Let's check the conditional operator? : in Perl which can be used to replace if...else statements. It has the following general form −SyntaxExp1 ? Exp2 : Exp3;Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon.The value of a? expression is determined like this: Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire? expression. If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression. Below is a simple example making use of this operator −Example Live Demo#!/usr/local/bin/perl $name = "Ali"; ... Read More

Add and Remove Elements in Perl Hashes

Mohd Mohtashim
Updated on 29-Nov-2019 05:59:22

901 Views

Adding a new key/value pair in a Perl hash can be done with one line of code using a simple assignment operator. But to remove an element from the hash you need to use delete function as shown below in the example −Example Live Demo#!/usr/bin/perl %data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40); @keys = keys %data; $size = @keys; print "1 - Hash size: is $size"; # adding an element to the hash; $data{'Ali'} = 55; @keys = keys %data; $size = @keys; print "2 - Hash size: is $size"; # delete the same ... Read More

Getting Hash Size in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 05:56:53

3K+ Views

You can get the size - that is, the number of elements from a hash in Perl by using the scalar context on either keys or values. Simply saying first you have to get an array of either the keys or values and then you can get the size of the array as follows −Example Live Demo#!/usr/bin/perl %data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40); @keys = keys %data; $size = @keys; print "1 - Hash size: is $size"; @values = values %data; $size = @values; print "2 - Hash size: is $size";OutputThis will produce the following ... Read More

Checking for Key/Value Existence in Perl Hash

Mohd Mohtashim
Updated on 29-Nov-2019 05:52:55

622 Views

If you try to access a key/value pair from a hash in Perl that doesn't exist, you'll normally get the undefined value, and if you have warnings switched on, then you'll get a warning generated at run time. You can get around this by using the exists function, which returns true if the named key exists, irrespective of what its value might be −Example Live Demo#!/usr/bin/perl %data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40); if( exists($data{'Lisa'} ) ) {    print "Lisa is $data{'Lisa'} years old"; } else {    print "I don't know age of Lisa"; ... Read More

Extracting Keys and Values from Hash in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 05:45:18

3K+ Views

You can get a list of all of the keys from a hash in Perl by using keys function, which has the following syntax −keys %HASHThis function returns an array of all the keys of the named hash. Following is the example −Example Live Demo#!/usr/bin/perl %data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40); @names = keys %data; print "$names[0]"; print "$names[1]"; print "$names[2]";OutputThis will produce the following result −Lisa John Paul KumarSimilarly, you can use values function to get a list of all the values. This function has the following syntax −Syntaxvalues %HASHThis function returns a normal ... Read More

Accessing Hash Elements in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 05:42:13

366 Views

When accessing individual elements from a hash in Perl, you must prefix the variable with a dollar sign ($) and then append the element key within curly brackets after the name of the variable. For example −Example Live Demo#!/usr/bin/perl %data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40); print "$data{'John Paul'}"; print "$data{'Lisa'}"; print "$data{'Kumar'}";OutputThis will produce the following result −45 30 40

Advertisements