Perl List

In perl programming list are used to combine single or multiple scalar values in a sequence. List element are defined within the parenthesis and each elements are separating by a comma. That element is form of an order sequences which of use by a index value.

#!/usr/bin/perl
package Regular;

#Here (1,2,3,4,5,6) is an list
#Accessing list element by index 

print ( (1,2,3,4,5,6)[0] ,"\n" ); #1
print ( (1,2,3,4,5,6)[5]  ,"\n" ); #6
print ( (1,2,3,4,5,6)[0..2] ,"\n" ); #123

In this example there are two type of list, One is outer list which is contain two element one is list and other is new line statement. Inner list are contains 6 elements (1,2,3,4,5,6) there is displayed by using indexes values.

Internally each multiple list is form of single list perl are automatically convert multi list to single one. In above example print operator are display as a single list result. For more clarification you'll can seen this example.

#!/usr/bin/perl
package Regular;
#define list
print (1,2,3,(4,5,(6,7,8))); 
print ("\n");
print (('a'),('e'),('i','o','u')); 
print ("\n");
print ((((((("1010"))),"1111")))); 
12345678
aeiou
10101111

qw() function also return a list by extracting each words of string. Let seen an example.

#!/usr/bin/perl
use warnings;
use strict;
#define list
print qw(Hello Programmer);
print "\n"; 
print qw(Hello Programmer)[0]; 
print "\n";
print qw{Hello Programmer}[1]; 
print "\n";

print qw[Hello Tester][1]; 
print "\n";

print qw /Are You Ready/[2]; 
print "\n";

print qw "For Code"[1]; 
print "\n";
HelloProgrammer
Hello
Programmer
Tester
Ready
Code


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved