Comprehensive Guide to Perl Arrays | Perl Programming

Appblee
2

Introduction 

• An array is a specialized type of variable that stores data in the form of a list. It is an orderly collection of scalar values.

• Each element can be accessed by its unique index number. The index number starts from 0 and ends with the maximum declared size of the array.

• In a Perl array you can store numbers, string floating values etc.

• Arrays are defined using the @ sign and to refer to a single element of it, the variable name proceeds with the $ sign.

• The reason for using $ is that there is a solitary value in the array, which is a scalar. The reason for this is that the single element in there is a single value in the array and it is a scalar.


Syntax:

@fruits = {'mango',' apple', 'banana', 'orange'};


for example:


• Also array can be declared using qw  .it stores data into an array considering a white space to be the delimiter.


Syntax:

@fruits = qw(orange  apple  mango  banana);

Perl array size or length

The size of an array is determined by the number of scalars on it. The size of the array must be one greater than the maximum index of the array. The maximum index can be obtained from $#array_name.


For example, if there is an array called fruits

@fruits = {'mango',' apple', 'banana', 'orange'};

maximum index of the fruits can be obtained from $#fruits

The scalar can be used to obtain the size or length of the array. The scalar is not a keyword but rather a built-in function.

scalar is ensured that the array is evaluated in the scalar context, and its size is returned instead of the list of elements.

Syntax:

@fruits = {'mango',' apple', 'banana', 'orange'};

#Explicit scalar context 

print("Size of the array: " . scalar@fruits);

#Implicit scalar context

$size = @fruits;

print("Size of the array: ".  $size);


    is used for concatenation.

-----------------------------------------------------------------------------------

Perl Functions

Some functions can be used to add and delete elements from an array.

Push on Array

-   adds an array element at the end of an existing array

Syntax:
    @fruits = {'mango',' apple', 'banana', 'orange'};
    push @fruits, "avocado";
    print("@fruits\n");

Output:
   mango apple banana orange avocado

Pop on Array

-   removes the last element from an array.

Syntax:
    @fruits = {'mango',' apple', 'banana', 'orange'};
    pop @fruits ;
    print("@fruits\n");

Output:
   mango apple banana 

Shift on Array

-   removes the first element from an array. 

Syntax:
    @fruits = {'mango',' apple', 'banana', 'orange'};
    push @fruits ;
    print("@fruits\n");

Output:
   mango apple banana 

Unshift on Array

-   adds an element at the beginning of an array.

Syntax:
    @fruits = {'mango',' apple', 'banana', 'orange'};
    push @fruits, "avocado";
    print("@fruits\n");

Output:
   avocado mango apple banana orange 







Array Compare

In Perl, there are two ways to check whether two arrays are equal. We can compare both by using the Storable Freeze function or by creating our own custom function.

Method 1: Using the Storable freeze function


The  freeze keyword along with the eq keyword to compare the array equality

Method 2: Using your own custom function



The equalArray method stores parameter values of array references using @_. This is a specialty unique to Perl, not found in other programming languages. It then checks if the two given arrays are identical by comparing the length of both. If the arrays have different lengths, the method returns 0 as they cannot be identical.

If the length of both arrays is equal, we declare the hash %hash and use the hash slice @hash{@array1,@array2} to assign an empty list ( ) of multiple keys in the hash. This ensures that only unique elements are in the hash when we combine both arrays. Thus, only the unique elements in @array1 and @array2 references will be in the hash and assigned as the keys. Then the values of the associated keys will be undef.

When we call the method we should pass array references as the arguments.

In conclusion here we provided a complete guide to how arrays work in Perl programming. Let's explore more about perl programming in the next post. 🍏🐝



Tags:

Post a Comment

2Comments

  1. Thanks for sharing this informative article about how much is important to have an android app for your business as we all know that android devices are in high demand it's very essential to Hire mobile application development company in india to take your buiness into the digital world and to reach your clients in an easy and secure way.

    mobile application development company in india
    mobile app development agency india
    mobile app development company in india
    mobile app development india

    ReplyDelete
Post a Comment