Array are variables that stored an ordered list of scalar values,we have already know bit about Array Data Type from previous post, But in this post we will discuss in detail of ARRAY Data Type which preceded by an “at” (@) sign.

let’s start declaring with one Array.

In below example, we are declaring two array and calling their scalar values with index to represents how we could make it presentable.

# cat variable_array 
#!/usr/bin/perl

@subject=( Math Science English Social-study Hindi);
@marks=(85,90,76,69,83);

print "$subject[0] = $marks[0]
$subject[1] = $marks[1]
$subject[2] = $marks[2]
$subject[3] = $marks[3]
$subject[4] = $marks[4]\n";

Now let’s see output out of this code.

# perl variable_array 
Math = 85
Science = 90
English = 76
Social-study = 69
Hindi = 83

Now we know how to call individual values from Array variables, we called it as scalar value but with index value, so during calling Array value, we regarded it as scalar.

As we saw above when we call any Array we used to define Array value within brackets but its not only way to define Array values, we can do same in through qw operator in below mentioned way.

#!/usr/bin/perl

@subject = qw/Math Science English Social-study Hindi/;
@marks=qw<85 90 76 69 83>;

print "$subject[0] = $marks[0]
$subject[1] = $marks[1]
$subject[2] = $marks[2]
$subject[3] = $marks[3]
$subject[4] = $marks[4]\n";

Above we saw that we used qw (quote word) to define Array variable. We just need to care while using qw because when we use opening character in start of Array with qw, we also need to use closing character while closing Array, like we did above (<>). There is also one limitation with qw Array that we can’t use array value with space, it will take it as two values.

Accessing Array values

To access to Array value , we have to use value index with $ sign as scalar variables, in above program we already mentioned access values from Array to print in another statements.

As usual in Computer, Array indexes also start from 0. So if you like to take first element from Array you have to fetch 0 index element. We can also use negative value (-1) but in this case we have to work from last element of array like below mentioned code.

root@jarvis:~/scripts/perl# cat variable_array 
#!/usr/bin/perl

@subject = qw/Math Science English Social-study Hindi/;
@marks=qw<85 90 76 69 83>;

print "$subject[0] = $marks[0]\n";
print "$subject[-1] = $marks[-1]\n";

root@jarvis:~/scripts/perl# perl variable_array 
Math = 85
Hindi = 83

In above code, we just print First and last element of Array. So we used both method positive index and negative index numbering.

Array Size

Sometime we need to know array size in such a way that during program we need to know elements used in Array Variables. We can do this with simple code mentioned below

root@jarvis:# cat variable_array 
#!/usr/bin/perl

@subject = qw/Math Science English Social-study Hindi/;

print "Max index value : $#subject\n";
print "Total number of Subjects :",scalar @subject," \n";
$array_size=@subject;
print "Array size of \@subject:", $array_size," \n";

root@jarvis:~# perl variable_array 
Max index value : 4
Total number of Subjects :5 
Array size of @subject:5 

So we tried to demonstrate it in three simple way. I hope it is clear to you. There are some other function in Perl which need to understand in terms Perl Array Variables, these are used to add and remove Array elements in Array variable.

Adding and Removing Elements in Array

We are trying to demonstrate how can add and remove elements from an Array variable. We will try to demo code for these functions.

1. Push

Push in Perl Array will add Another element or list at end of existing array, its syntax is quit simple push @ARRAY, LIST. Let’s see some example of Push in Perl code.

root@jarvis:~/scripts/perl# cat variable_array 
#!/usr/bin/perl

@subject = qw/Math Science English Social-study Hindi/;

print "Current \@subject Array list:\t @subject\n";
print  "Number of Elements before push :", scalar @subject, "\n";
push @subject,'French';
print "New \@subject Array list:\t  @subject\n";
print  "Number of Elements before push :", scalar @subject, "\n";
 
root@jarvis:~/scripts/perl# perl variable_array 
Current @subject Array list:	 Math Science English Social-study Hindi
Number of Elements before push :5
New @subject Array list:	  Math Science English Social-study Hindi French
Number of Elements before push :6

In above code we can see we added one more element, in same way we can add multiple element or entire list at end of Array variable. Here good thing is that we need not to reassign or assign another variable for push, we just need to do push on Array variable.

2. Pop

Like in Push, pop does completely opposite thing with Array variable. Like push add in end of array variable, but Pop remove last element from Array variable. Syntax is simple pop @ARRAY. Let’s take an example of this with Perl code.

!/usr/bin/perl

@subject = qw/Math Science English Social-study Hindi/;

print "Current \@subject Array list:\t @subject\n";
print  "Number of Elements before push :", scalar @subject, "\n";
pop  @subject;
print "New \@subject Array list:\t  @subject\n";
print  "Number of Elements before push :", scalar @subject, "\n";

root@jarvis:~/scripts/perl# perl variable_array 
Current @subject Array list:	 Math Science English Social-study Hindi
Number of Elements before push :5
New @subject Array list:	  Math Science English Social-study
Number of Elements before push :4

Like we did operation at end of Array variable we can do same addition and removal from start of Array variable. For this we have shift and unshift functions. Let’s discuss them in detail.

3.Shift
Shift is also do removal on element from Array variable but from start of it. So it will remove 0 index element from Array variable.

root@jarvis:~/scripts/perl# cat variable_array 
#!/usr/bin/perl

@subject = qw/Math Science English Social-study Hindi/;

print "Current \@subject Array list:\t @subject\n";
print  "Number of Elements before push :", scalar @subject, "\n";
shift  @subject;
print "New \@subject Array list:\t  @subject\n";
print  "Number of Elements before push :", scalar @subject, "\n";

root@jarvis:~/scripts/perl# perl variable_array 
Current @subject Array list:	 Math Science English Social-study Hindi
Number of Elements before push :5
New @subject Array list:	  Science English Social-study Hindi
Number of Elements before push :4

In Above code shift function remove first element of Array variable.

4.UnShift
Like push unshift will also add one element in Array variable but on first position of element.Let’s see with Perl code.

root@jarvis:~/scripts/perl# cat variable_array 
#!/usr/bin/perl

@subject = qw/Math Science English Social-study Hindi/;

print "Current \@subject Array list:\t @subject\n";
print  "Number of Elements before push :", scalar @subject, "\n";
unshift  @subject,French;
print "New \@subject Array list:\t  @subject\n";
print  "Number of Elements before push :", scalar @subject, "\n";

root@jarvis:~/scripts/perl# perl variable_array 
Current @subject Array list:	 Math Science English Social-study Hindi
Number of Elements before push :5
New @subject Array list:	  French Math Science English Social-study Hindi
Number of Elements before push :6

Replacing Array Elements

This function is used to identified Elements in Array and replace them from another elements. We think this could be more clear if we demonstrate some codes like below mentioned.

root@jarvis:~/scripts/perl# cat 1_variable_array 
#!/usr/bin/perl

@num=(1..20);

print "Old Numbers -- @num\n";

splice (@num, 4 ,6 ,60..65);

print "New numbers -- @num\n";
root@jarvis:~/scripts/perl# perl 1_variable_array 
Old Numbers -- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
New numbers -- 1 2 3 4 60 61 62 63 64 65 11 12 13 14 15 16 17 18 19 20

Now we hope it is more clear to know how it works. Here we replace some elements from another elements, its not compulsory to replace some length of elements in Array, we can replace more or less elements as per requirements.

Sort Array

We can use sort function to sort elements in Array variables which would more appropriate from some required outputs. It would work like in code as below mentioned.

root@jarvis:~/scripts/perl# cat 1_variable_array 
#!/usr/bin/perl

@num=(1..20);

print "Old Numbers -- @num\n";

splice (@num, 4 ,6 ,60..65);
@sort_num=sort @num;
print "New numbers -- @sort_num \n";

root@jarvis:~/scripts/perl# perl 1_variable_array 
Old Numbers -- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
New numbers -- 1 11 12 13 14 15 16 17 18 19 2 20 3 4 60 61 62 63 64 65 

Scalar to Array and Array to Scalar

As in any other computer language like python in Perl as perl as well we can interchange data type like any Scalar variable could work like Array or Array would work like scalar. For this we are trying to draft a piece of code which has an example of both functions which are responsible for them.

Syntax for same is quit simple, we only need to decide separated or delimiter expression used for them. I hope it would be more clear after code.

root@jarvis:~/scripts/perl# cat 2_array_variable
#!/usr/bin/perl

$scalar_1="this_is_an_scalar_value";
@array_1=qw/this,is,an,array,value/;

@array_2=split('_',$scalar_1);
$scalar_2=join(',',@array_1);

print "\@array_2 : @array_2\n\n";
print "Data type for \@array_2 : ", ref(\@array_2), "\n\n";
print "\$scalar_2: $scalar_2\n\n";
print "Data type for \@scalar_2 : ", ref(\$scalar_2), "\n\n";

root@jarvis:~/scripts/perl# perl 2_array_variable 
@array_2 : this is an scalar value

Data type for @array_2 : ARRAY

$scalar_2: this,is,an,array,value

Data type for @scalar_2 : SCALAR

In Above code we can see scalar_1 and array_1 has scalar and array Data Type that has been interchange. We also used another Perl function in this code named ref(), which used to determine variable Data Type.

Next