1/11/2022

Data Structure in PERL : Array and Context


In this article, we explored the fundamentals of basic data structures in PERL, focusing on the versatile Array/List. We began by introducing arrays, covering their creation and methods for populating them with elements. Next, we demonstrated efficient techniques for accessing and manipulating array data. We also explored advanced topics, including Array of Arrays for creating multi-dimensional structures and Sub Arrays for extracting specific subsets. Additionally, we reviewed various Array Operators with real-world examples to illustrate their functionality. Our discussion on Context covered Scalar and List Context, highlighting how they influence array behavior and other data structures in PERL. By the episode’s end, readers gained a solid understanding of arrays, lists, and the critical concept of context for effective programming.

PERL has inbuilt data-structures. In this article we will be covering them. In PERL, generically any data structure is called as list. However we can make two broad classification as below :


In this article we will be focusing on Array . In the next article we will be focusing on Associative Array or Hash.

Arrays in PERL :

  • In PERL the array is much easier compared to the array in C-Language. Now let us understand how the array is organised in PERL : 
  • Array/List variables always start with @ (the “at” symbol).
  • Arrays/List are variable length. 
  • Array/List can be declared without length.
  • This length can vary even after creation.
  • Array/List can contain any sorts of scalars (even a combination of strings, numeric, etc.).
  • Array/List elements are written with parentheses.
  • Use “my” whenever creating the array/list.


Creation of List/Array in PERL :

We will understand the array creation by code-example as :

The above example is self explanatory .

Accessing an Array/List :

After we have created array , now it is the time to access the array. 

  • Each array element is accessed as  $arr[index]
  • my $ref = \@arr ; #gives the reference (pointer) to an array
  • my $length = scalar(@arr) ; #gives the array length
  • my $LastIndex = $#arr # gives the index of the last element of array
  • $arr[-1] #gives the last element of @arr
  • Negative indices count backward from the end of an array
  • my @arr = (1, 2, 3,4); # four  values in the array
  • my $ele1 = $arr[0]; # $ele1 gets the 1
  • my $ele2 = $arr[1]; # $ele2 gets the 2
  • ($arr[0],$arr[1]) = ($arr[1],$arr[0]); # swaps first two elements

The Array of Array :

The array structure can be nested in the below ways :

  • my @array1 = (1, 2, 3); #first array
  • my @array2 = (4, 5, 6); #second array
  • my @array3 = (@array1, @array2); #merged array
  • my @list1 = (1, 2, 3); # three values
  • my @list2 = @list1; # all three values are copied
  • my @list3 = (4, 5, @list2, 6); # six values

Sub-Array or Array Slicing :

A bigger array can be cut/slice in into smaller array in the below way :

  • my $arr = qw(apple orange jackfruit grape mango);
  • @arr[2,4] # is not a two dimensional array, it is ($a[2], $a[4]);
  • my @subArray1 = @arr[2,4] # actually is ("jackfruit" "mango") ;
  • my @subArray2 = @arr[2..4] # actually is ("jackfruit" "grape" "mango");

Miscellaneous Array Operations :

Apart from the major operations , we can do some small operation on array as :

  • push(@list, $new); # add to end of list. Same as @list = (@list, $new);
  • $old = pop(@list); # removes rightmost value
  • unshift(@list, $new); # insert at beginning. Same as @list = ($new, @list);
  • $old = shift(@list); # removes leftmost value. Same as ($old, @list) = @list;
  • @arr = reverse(@arr2); # @arr is the reverse of @arr2
  • @arr = sort(@arr2); # sorts on string (ASCII/Alphabetic Order) values
  • @sorted = sort { $a <=> $b } @unsorted; # sorts integer/real (ascending Order) 
  • @sorted = sort { $b <=> $a } @unsorted; # sorts integer/real (descending Order)
  • chomp(@arr); # chomps each element of @arr

Array Example :

With the below code , we have exemplified the discussed array operations.

When executed , the above code block gives the below output :


Scalar & List Context in PERL :

Perl has a notion of two different “contexts” in which evaluations can take place: 

scalar context and list context.

Things behave differently depending on what context they’re in.

An expression is in “scalar context” if the value it generates will be used as a scalar. 

An expression is in “list context” if the value it generates will be used as a list (i.e. an array or hash). 

  • my @things = (’peach’, 2, ’apple’, 3.1415927);
  • my @thing1 = @things;
  • my $thing2 = @things;
  • my ($thing3) = @things;
  • print "\@thing1: @thing1\n";
  • print "\$thing2: $thing2\n";
  • print "\$thing3: $thing3\n";

my ($first, $second) = @array;

$first and $second are assigned the first and second elements of @array,

my ($first, @rest) = @array;

$first is assigned the first element of @array, and @rest is assigned everything else.

my (@copy, $dummy) = @array;

In this case $dummy is not assigned.

Arrays are variable-length, so @copy has no reason to stop.

Hence  $dummy ends up with undef since @copy “eats up” the entire available array. 

Watch the video here:



Courtesy : Image by Jae Rue from Pixabay