Broad Network


One-Dimensional Array Functions on Perl Two-Dimensional Array

Perl Multi-Dimensional Array - Part 2

Forward: In this part of the series, I explain how Perl one-dimensional array functions can be used on Perl 2D array.

By: Chrysanthus Date Published: 2 Dec 2012

Introduction

This is part 2 of my series, Perl Multi-Dimensional Array. In this part of the series, I explain how Perl one-dimensional array functions can be used on Perl 2D array. You should have read the previous part of the series before coming here, as this is a continuation.

Length of a Two-Dimensional Array
The length of a one-dimensional array is obtained by assigning the array name, preceded by the @ symbol, to a variable. When you do this for a two-dimensional array, you obtain the number of rows. Read and try the following program:

use strict;

    my @arr;

    $arr[0][0] = "ProductID";
    $arr[0][1] = "ProductName";
    $arr[0][2] = "Category";
    $arr[1][0] = 1;
    $arr[1][1] = "TV Set";
    $arr[1][2] = "Entertainment";

    my $arrLength = @arr;

    print $arrLength;

In the case of array by initialization, you use the following syntax for the array identifier, to obtain the length:

    @$arrayReferenceName

The following program illustrates this:

use strict;

   my $twoDArrRef;

    $twoDArrRef = [
                     ["ProductID", "ProductName", "Category"],
                     [1, "TV Set", "Entertainment"]
                 ];

    my $arrLength = @$twoDArrRef;

    print $arrLength;

Slicing a 2D Array by Rows
Slicing a 2D Array by Rows means obtaining a range of rows in the 2D array. To do this, you begin with the @ sign. This is followed by the name of the array; then square brackets. Inside the square brackets, you begin with the index of the start row of the range, then two dots and then the index of the end row of the range. Here, a range means consecutive rows in the array. If the number of elements in the range is small, say two, three or four, then you can type their indices in the square brackets separating them with commas and you would not need the two dots.

The return object is an array (the slice). The return new array can be assigned to a new array variable. Nothing is deleted in the original array. The return array is a 2D array and you would access it as you would access other two-dimensional arrays. Read and try the following program that illustrates this:

use strict;

    my @arr;

    $arr[0] = ["ProductID", "ProductName", "Category", "Number", "CostPrice", "SellingPrice"];
    $arr[1] = [1, "TV Set", "Entertainment", 50, 25, 30];
    $arr[2] = [2, "DVD", "Entertainment", 50, 20, 25];
    $arr[3] = [3, "Clothe Box", "Household", 45, 16, 21];
    $arr[4] = [4, "Perfume", "Beauty", 100, 2, 3];
    $arr[5] = [5, "Banana", "Fruit", 125, 5, 7];
    $arr[6] = [6, "Pear", "Fruit", 135, 3, 4];

    my @slicedArr = @arr[2..5];
    print $slicedArr[0][3], "\n";
    print $slicedArr[1][3], "\n";
    print $slicedArr[2][3], "\n";
    print $slicedArr[3][3], "\n";

Array Functions
One-dimensional array functions can be used with two-dimensional arrays, row-by-row. I assume that you have been reading the tutorials in this course (see below) in the order given. If you have not been doing that, then you may have problems understanding this tutorial.

Pushing A Row
You can add a row at the end of a 2D array. You use the 1D push function. The row added is a 1D array. The row argument to the function is a reference to the 1D array row, which is to be added.  A reference to a named 1D array can be got as follows:

    @arr = ("one", "two", 3, 4);
    $aref = \@arr;

A reference to an anonymous 1D array can be got as follows:

    $aref = ["one", "two", 3, 4];

where $aref is the variable holding the reference.

The syntax of the push function for adding a 1D array row at the bottom of a 2D array, is:

    push(ARRAY, Reference);

In the following program, two rows are added to a 2D array, using the push function twice:

use strict;

    my @arr;

    $arr[0] = ["ProductID", "ProductName", "Category", "Number", "CostPrice", "SellingPrice"];
    $arr[1] = [1, "TV Set", "Entertainment", 50, 25, 30];
    $arr[2] = [2, "DVD", "Entertainment", 50, 20, 25];
    $arr[3] = [3, "Clothe Box", "Household", 45, 16, 21];
    $arr[4] = [4, "Perfume", "Beauty", 100, 2, 3];

    my @rowE = (5, "Banana", "Fruit", 125, 5, 7);
    my $aref1 = \@rowE;

    my $aref2 = [6, "Pear", "Fruit", 135, 3, 4];

    push(@arr, $aref1);
    push(@arr, $aref2);

    print $arr[5][1], "\n";
    print $arr[6][1], "\n";

The push function returns the new number of rows in the array; this number is the old number plus 1 for any push function call.

Unshifting a Row
Unshifting does the opposite of pushing. It prepends a row in front of a 2D array, returning the new number of rows in the resulting array. The syntax is:

    unshift (ARRAY, Reference);

In the following program, a row is unshifted to the beginning of a 2D array:

use strict;

    my @arr;

    $arr[0] = [1, "TV Set", "Entertainment", 50, 25, 30];
    $arr[1] = [2, "DVD", "Entertainment", 50, 20, 25];
    $arr[2] = [3, "Clothe Box", "Household", 45, 16, 21];
    $arr[3] = [4, "Perfume", "Beauty", 100, 2, 3];
    $arr[4] = [5, "Banana", "Fruit", 125, 5, 7];

    my $aref1 = ["ProductID", "ProductName", "Category", "Number", "CostPrice", "SellingPrice"];

    unshift (@arr, $aref1);

    print $arr[0][0], "\n";

Popping a Row
The pop function removes the last row from the array and returns the reference of the removed row, shortening the original array by one row. The syntax is,

    pop (ARRAY);

In the following program the last row is popped out:

use strict;

    my @arr;

    $arr[0] = ["ProductID", "ProductName", "Category", "Number", "CostPrice", "SellingPrice"];
    $arr[1] = [1, "TV Set", "Entertainment", 50, 25, 30];
    $arr[2] = [2, "DVD", "Entertainment", 50, 20, 25];
    $arr[3] = [3, "Clothe Box", "Household", 45, 16, 21];
    $arr[4] = [4, "Perfume", "Beauty", 100, 2, 3];
    $arr[5] = [5, "Banana", "Fruit", 125, 5, 7];
    $arr[6] = [6, "Pear", "Fruit", 135, 3, 4];

    my $oldArrayLength = @arr;
    print $oldArrayLength, "\n";

    my $poppedArrRef = pop(@arr);

    print $$poppedArrRef[1], "\n";

    my $newArrayLength = @arr;
    print $newArrayLength, "\n";

Do read and try all the code samples of this tutorial.

Shifting a Row
The shift function does the opposite of the pop function. It removes the first row from the array and returns the reference of the removed row, shortening the original array by one row. The syntax is,

    shift (ARRAY);

In the following program the first row is shifted out:

use strict;

    my @arr;

    $arr[0] = ["ProductID", "ProductName", "Category", "Number", "CostPrice", "SellingPrice"];
    $arr[1] = [1, "TV Set", "Entertainment", 50, 25, 30];
    $arr[2] = [2, "DVD", "Entertainment", 50, 20, 25];
    $arr[3] = [3, "Clothe Box", "Household", 45, 16, 21];
    $arr[4] = [4, "Perfume", "Beauty", 100, 2, 3];
    $arr[5] = [5, "Banana", "Fruit", 125, 5, 7];
    $arr[6] = [6, "Pear", "Fruit", 135, 3, 4];

    my $oldArrayLength = @arr;
    print $oldArrayLength, "\n";

    my $shiftedArrRef = shift(@arr);

    print $$shiftedArrRef[1], "\n";

    my $newArrayLength = @arr;
    print $newArrayLength, "\n";

Splicing a Range of Rows
The splice function removes a portion (range) of rows from the array and replaces it with that of some other 2D array; if that is present. The resulting array may be larger or smaller than the original array depending on how many rows are in the replacement 2D Array. If there is no replacement array, then the resulting array is shorter. The splice function returns the 2D array of the elements removed. The syntax is,

    splice (ARRAY, OFFSET, LENGTH, REPLACEMENTARRAY)

ARRAY is the name of the original array. OFFSET is the index at which the removal will start. Remember, index counting begins from zero. LENGTH is the number of consecutive rows to be removed. REPLACEMENTARRAY, if present has the rows to replace the ones removed.

For our example, imagine that you want to remove 3 rows beginning from index 2 (which is position 3) and replace them with the following array:

    my @arrReplace;

    $arrReplace [0] = ["2,0", "2,1", "2,2", "2,3", "2,4", "2,5"];
    $arrReplace [1] = ["3,0", "3,1", "3,2", "3,3", "3,4", "3,5"];
    $arrReplace [2] = ["4,0", "4,1", "4,2", "2,3", "4,4", "4,5"];


The splice function would be,

    splice (@arr, 2, 3, @arrReplace);

So, 3 rows will be removed beginning from index 2. The LENGTH is 3.

The following program illustrates this:

use strict;

    my @arr;

    $arr[0] = ["ProductID", "ProductName", "Category", "Number", "CostPrice", "SellingPrice"];
    $arr[1] = [1, "TV Set", "Entertainment", 50, 25, 30];
    $arr[2] = [2, "DVD", "Entertainment", 50, 20, 25];
    $arr[3] = [3, "Clothe Box", "Household", 45, 16, 21];
    $arr[4] = [4, "Perfume", "Beauty", 100, 2, 3];
    $arr[5] = [5, "Banana", "Fruit", 125, 5, 7];
    $arr[6] = [6, "Pear", "Fruit", 135, 3, 4];

    my @arrReplace;

    $arrReplace[0] = ["2,0", "2,1", "2,2", "2,3", "2,4", "2,5"];
    $arrReplace[1] = ["3,0", "3,1", "3,2", "3,3", "3,4", "3,5"];
    $arrReplace[2] = ["4,0", "4,1", "4,2", "2,3", "4,4", "4,5"];

    my @removedArr = splice (@arr, 2, 3, @arrReplace);

    for (my $i=0; $i<7; ++$i)
        {
            for (my $j=0; $j<6; ++$j)
                {
                    print $arr[$i][$j] . ", ";
                }
            print "\n";        
        }

    print "\n";

    for (my $i=0; $i<3; ++$i)
        {
            for (my $j=0; $j<6; ++$j)
                {
                    print $removedArr[$i][$j] . ", ";
                }
            print "\n";        
        }

Do not forget to read and try the code.

That is it for this part of the series. We take a beak here and continue in the next part.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message