Perl Tutorial

Fundamentals

Input and Output

Control Flow

Arrays and Lists

Hash

Scalars

Strings

Object Oriented Programming in Perl

Subroutines

Regular Expressions

File Handling

Context Sensitivity

CGI Programming

Misc

Array functions in Perl

Arrays are one of the fundamental data structures in Perl. There are several built-in functions to manipulate and inspect arrays. This tutorial will cover some of the essential array functions in Perl.

1. Introduction

In Perl, arrays are ordered collections of scalars indexed by number, starting with 0. They are dynamic and can grow and shrink. Perl provides a plethora of functions to aid in working with arrays.

2. push and pop

  • push: Adds one or more elements to the end of an array.
  • pop: Removes and returns the last element of an array.
my @arr = (1, 2, 3);
push @arr, 4, 5;     # @arr is now (1, 2, 3, 4, 5)
my $last = pop @arr; # @arr is now (1, 2, 3, 4) and $last is 5

3. shift and unshift

  • shift: Removes and returns the first element of an array.
  • unshift: Adds one or more elements to the beginning of an array.
my @arr = (2, 3, 4);
unshift @arr, 1;      # @arr is now (1, 2, 3, 4)
my $first = shift @arr; # @arr is now (2, 3, 4) and $first is 1

4. splice

splice can be used to replace a section of an array with other values or to extract a section of an array.

my @arr = (1, 2, 5, 6);
splice @arr, 2, 0, (3, 4);       # Inserts 3 and 4 before the 3rd element. @arr is now (1, 2, 3, 4, 5, 6)

my @removed = splice @arr, 2, 2; # Removes 2 elements from the 3rd position. @arr is now (1, 2, 5, 6) and @removed is (3, 4)

5. sort

Sorts an array. By default, it sorts in standard string comparison order, but you can provide a custom comparison.

my @numbers = (4, 1, 8, 3);
my @sorted = sort { $a <=> $b } @numbers; # @sorted is (1, 3, 4, 8)

6. reverse

Reverses the elements of an array.

my @arr = (1, 2, 3);
my @reversed = reverse @arr; # @reversed is (3, 2, 1)

7. grep

Finds elements in an array that match a condition.

my @numbers = (1, 2, 3, 4, 5);
my @evens = grep { $_ % 2 == 0 } @numbers; # @evens is (2, 4)

8. map

Transforms elements of an array.

my @numbers = (1, 2, 3);
my @squared = map { $_**2 } @numbers; # @squared is (1, 4, 9)

9. scalar

Returns the number of elements in an array when used in scalar context.

my @arr = (1, 2, 3, 4);
my $count = scalar @arr; # $count is 4

10. Summary

Perl's array functions offer a powerful set of tools to manipulate and inspect arrays. Understanding and mastering these functions enable effective and efficient array handling, which is pivotal for many tasks in Perl.

  1. Manipulating arrays in Perl:

    • Description: Basic array manipulation in Perl.
    • Code Example:
      # Creating an array
      my @colors = ('red', 'green', 'blue');
      
      # Accessing elements
      print "First color: $colors[0]\n";
      
      # Modifying elements
      $colors[1] = 'yellow';
      
      # Printing the modified array
      print "Modified colors: @colors\n";
      
  2. Adding elements to arrays in Perl:

    • Description: Adding elements to the end of an array using push.
    • Code Example:
      my @fruits = ('apple', 'banana');
      push @fruits, 'orange', 'grape';
      
      # Printing the updated array
      print "Updated fruits: @fruits\n";
      
  3. Removing elements from arrays in Perl:

    • Description: Removing elements from an array using pop and shift.
    • Code Example:
      my @numbers = (1, 2, 3, 4);
      pop @numbers;  # Remove the last element
      shift @numbers;  # Remove the first element
      
      # Printing the updated array
      print "Updated numbers: @numbers\n";
      
  4. Searching arrays in Perl:

    • Description: Searching for elements in an array using grep and first.
    • Code Example:
      my @names = ('Alice', 'Bob', 'Charlie');
      my @found_names = grep { /b/i } @names;  # Case-insensitive search
      
      # Printing the found names
      print "Found names: @found_names\n";
      
  5. Sorting arrays in Perl:

    • Description: Sorting elements in an array using sort.
    • Code Example:
      my @scores = (30, 15, 50, 25);
      my @sorted_scores = sort @scores;
      
      # Printing the sorted array
      print "Sorted scores: @sorted_scores\n";
      
  6. Iterating over arrays in Perl:

    • Description: Iterating over array elements using foreach.
    • Code Example:
      my @animals = ('lion', 'elephant', 'zebra');
      foreach my $animal (@animals) {
          print "Animal: $animal\n";
      }
      
  7. Slicing arrays in Perl:

    • Description: Extracting a portion of an array using array slicing.
    • Code Example:
      my @numbers = (1, 2, 3, 4, 5);
      my @subset = @numbers[1..3];
      
      # Printing the sliced array
      print "Sliced subset: @subset\n";
      
  8. Joining arrays in Perl:

    • Description: Joining array elements into a string using join.
    • Code Example:
      my @fruits = ('apple', 'orange', 'banana');
      my $fruit_string = join(', ', @fruits);
      
      # Printing the joined string
      print "Fruits: $fruit_string\n";
      
  9. Array functions vs. array operators in Perl:

    • Description: Understanding the difference between array functions and operators.
    • Code Example:
      my @numbers = (3, 1, 4, 1, 5, 9);
      my $count = scalar @numbers;  # Function
      my $sum = 0;
      
      # Using an operator
      foreach my $num (@numbers) {
          $sum += $num;
      }
      
      # Printing the count and sum
      print "Count: $count, Sum: $sum\n";