Perl | List Context Sensitivity
Last Updated :
14 Mar, 2019
Introduction
In Perl, function calls, terms, and statements have inconsistent explications which rely upon its Context. There are two crucial Contexts in Perl, namely List Context and Scalar Context. In a list context, Perl gives the list of elements. But in a scalar context, it returns the number of elements in the array.
Perl presumes List values in “List Context”, where as the list could have any number of elements or it can have only one element or it can even be desolated.
Creating List Context
List Context can be generated with the use of Arrays and Lists.
- Assignment to an array:
Example:
@y = LIST;
@y = @z;
@y = localtime();
Here, localtime() is a function name in Perl, which unveils number depiction of time in array.
- Assignment to a list:
Example:
($x, $y) = LIST;
($x) = LIST;
Here, List can moreover create List Context even if the List has only one element.
Example:
my @CS = ( 'geeks' , 'for' , 'geeks' , 'articles' );
my ( $x , $y ) = @CS ;
my @z = @CS ;
my @t = localtime ();
print "$x $y\n" ;
print "@z\n" ;
print @t ;
|
Output:
geeks for
geeks for geeks articles
201761121191690
Here, in assignment to List there are two scalars in the List i.e, $x and $y, so only two elements of the array are assigned.
Arrays in List Context:
In order to provoke List Context using an array, we need to assign an array to another array.
Example:
use strict;
use warnings;
use 5.010;
my @x = ( 'computer_' , 'science_' , 'portal_' ,
'for_' , 'GeeksforGeeks' );
my @y = @x ;
print @y ;
|
Output:
computer_science_portal_for_GeeksforGeeks
Here, elements from one array are copied to another array.
Use of if-statement in List Context
if-statement is used in a List context to display the statements enclosed within ‘if’ only when there are elements present in the array.
Example:
use strict;
use warnings;
use 5.010;
my @x = ( 'G' , 'f' , 'G' );
if ( @x )
{
print "GeeksforGeeks" ;
}
|
Here, if the stated array has some number of elements then if-condition is true and it provokes the content of the if-statement but if the array is empty then the if-condition is false so it does not execute the statements within if-statement.
Reading in List Context:
“STDIN” is a readline operator in Perl. In order to place readline operator in List Context it is required to designate this operator to an array.
Example:
use strict;
use 5.010;
print "Enter the list of names:\n" ;
my @y = <STDIN>;
chomp @y ;
print "The number of names are: " .
scalar ( @y ) . "\n" ;
|
Output:
Here is how this program works:
Step 1: Enter the names which are to be stored in the array one by one with the use of enter key.
Step 2: Press Ctrl-D in Linux systems whereas Ctrl-Z in Windows system to indicate the ending of input.
Step 3: chomp is used to remove the extra line added after each input.
Step 4: Printing the number of elements in the array with the use of scalar because “an array in Scalar Context” can only return the length of an array.
Share your thoughts in the comments
Please Login to comment...