Perl Array

Array is capable to combine multiple element into single compound value. Each element of array are get by index value. By default first element of array are start at index zero. And next upcoming element is increase one by one.

#!/usr/bin/perl
package Regular;
use strict;
use warnings;

#Array of integer element
our @first = (1,2,3,4);

#Array of string literal
our @second=("one","two","three");

#Array of scalar literals value
our @three =("one",2,3.10);
Perl Array Example

In this example there are create three different array in Regular package. Array variable can be defined in perl programming using @ sign.

Normally there are two ways to define array elements. Fist is list elements (defining array element inside parentheses separating by comma) and second can be anonymous array.

#!/usr/bin/perl
package Regular;
use strict;
use warnings;


our @list=("A","B","C");

our @anonymous =["A","B","C"];

#Display array element
print @list,"\n";

#Display address of anonymous array
print @anonymous,"\n";

#get first element of both array
print $list[0],"\n";
print $anonymous[0][0],"\n";
 Creating array using anonymous or list element
Output
ABC
ARRAY(0x25c8810)
A
A

Note that anonymous array are create an inner array(like a 2d array). And list element are create 1d array. Both are useful. Generally list elements are not create multidimensional array. And anonymous elements are used to create multidimensional array. For example.

#!/usr/bin/perl
package Regular;
use strict;
use warnings;


our @list=(1,2,3,(4,5,6,(7,8)));

#Display array element
print @list,"\n";

print $list[0],"\n";
print $list[1],"\n";
print $list[2],"\n";
print $list[3],"\n";
print $list[4],"\n";
print $list[5],"\n";
print $list[6],"\n";
print $list[7],"\n";
Creating array using list element
12345678
1
2
3
4
5
6
7
8

In other side, when create an array using anonymous elements.

#!/usr/bin/perl
package Regular;
use strict;
use warnings;


our @anonymous=[1,2,3,[4,5,6,[7,8]]];

#Display array element
print @anonymous,"\n";

print $anonymous[0][0],"\n";
print $anonymous[0][1],"\n";
print $anonymous[0][2],"\n";
print $anonymous[0][3][0],"\n";
print $anonymous[0][3][1],"\n";
print $anonymous[0][3][2],"\n";
print $anonymous[0][3][3][0],"\n";
print $anonymous[0][3][3][1],"\n";
Creating array using anonymous element
ARRAY(0xca4960)
1
2
3
4
5
6
7
8

There are also possible to combine list and anonymous elements. list are creating single dimensional and anonymous elements can create multidimensional array. See this example and think about above example.

#!/usr/bin/perl
package Regular;
use strict;
use warnings;


our @elements=(1,2,3,[4,5,6,[7,8]]);

#Display array element
print @elements,"\n";

print $elements[0],"\n";
print $elements[1],"\n";
print $elements[2],"\n";
print $elements[3][0],"\n";
print $elements[3][1],"\n";
print $elements[3][2],"\n";
print $elements[3][3][0],"\n";
print $elements[3][3][1],"\n";
Combine list and anonymous elements
123ARRAY(0x2446840)
1
2
3
4
5
6
7
8

Copy Array

#!/usr/bin/perl
package Regular;
use strict;  
use warnings; 


our @original = (1,2,3,4);

#copy array elements
our @copy = @original;
Copy Array Elements

In this example @original are copied that element into another array @copy when @original are assigns. That is a way to copy whole array whatever the array is single or a multidimensional.

If array is multidimensional and you'll are trying to copy a particular element value which is form of an inner array. Then the above example are not creating new array (not copy array element) to inner array. There are assigning of the inner array reference to variable. See this example.

#!/usr/bin/perl
package Regular;
use strict;  
use warnings; 

use Data::Dumper;

our @arr = ("One","Two",[1,2,3,4]);

#array which is hold the reference of inner array
our @inner = $arr[2];
print "Before\n";
print "\@arr : ", Dumper(\@arr ),"\n";
print "\@inner : ",Dumper(\@inner ),"\n";

print "------------------------\n";
print "After\n";
#when update
$inner[0][0]=11;
$inner[0][1]=22;

print "\@arr : ", Dumper(\@arr ),"\n";
print "\@inner : ",Dumper(\@inner ),"\n";
Inner array reference example in perl
Before
@arr : $VAR1 = [
          'One',
          'Two',
          [
            1,
            2,
            3,
            4
          ]
        ];

@inner : $VAR1 = [
          [
            1,
            2,
            3,
            4
          ]
        ];

------------------------
After
@arr : $VAR1 = [
          'One',
          'Two',
          [
            11,
            22,
            3,
            4
          ]
        ];

@inner : $VAR1 = [
          [
            11,
            22,
            3,
            4
          ]
        ];

In this example $arr[2] is storing an address of array. when $arr[2] are assign to @inner in this case it will point to array (store the address of array). In this example are not copy actual array. So we can solve this problem using de-referencing. See the solution.

#!/usr/bin/perl
package Regular;
use Data::Dumper;

@arr = ("One","Two",[1,2,3,4]);

#copy inner array
@inner = @{@arr[2]};
print "Before\n";
print "\@arr : ", Dumper(\@arr ),"\n";
print "\@inner : ",Dumper(\@inner ),"\n";

print "------------------------\n";
print "After\n";
#when update
$inner[0]=11;
$inner[1]=22;

print "\@arr : ", Dumper(\@arr ),"\n";
print "\@inner : ",Dumper(\@inner ),"\n";
Copy inner array element in perl
Before
@arr : $VAR1 = [
          'One',
          'Two',
          [
            1,
            2,
            3,
            4
          ]
        ];

@inner : $VAR1 = [
          1,
          2,
          3,
          4
        ];

------------------------
After
@arr : $VAR1 = [
          'One',
          'Two',
          [
            1,
            2,
            3,
            4
          ]
        ];

@inner : $VAR1 = [
          11,
          22,
          3,
          4
        ];

In this example @arr[2] is point to inner arr and @{@arr[2]} are returning a new copy of a inner array elements.

Array reference

#!/usr/bin/perl
package Regular;

@original = ("One","Two","Three","Four");

@ref1 = \@original;

@ref2 = \@ref1;

@ref3 = \@ref2;

@ref4 = \@ref3;

print $ref4[0][0][0][0][0];#one
Perl Array reference example

Multidimensional Array

#!/usr/bin/perl
package Regular;

@first=(1,2,3);
@second=("A","B");
@third=(10,50,100,1000);

@arr=(\@first,\@second,\@third);

print $arr[0][0],"\n"; #1
print $arr[1][0],"\n"; #A
print $arr[2][0],"\n"; #10
Example Multidimensional Array

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







Kalvin     9 Day ago
#Sokolov you notice Good point, This is visualize how to access array element in different scenarios and it is useful to me. 
Sokolov Yura     11 Day ago
There is no need in storing array reference in another array.

You'd better read and link official documentation:
https://perldoc.perl.org/perlref
https://perldoc.perl.org/perlref#Square-Brackets
Sokolov Yura     11 Day ago
Anonymous array? Why did you put array reference into array and then called it "anonymous"? It is just array reference in the array, nothing more.

© 2021, kalkicode.com, All rights reserved