This chapter demonstrates about the Perl Array which are used to store an ordered list of scalar values and which are preceded by '@' sign and following are the concepts are covered in this chapter.
About Perl Array
Sequential Array
Dynamic Array
About Perl Array
Description
Array is a special type of variable is used to store the data in the form of a list in which element can be accessed by using the index number and which is unique for each element. In Array, a user can store strings, numbers, floating values etc. In Perl user can define an Array by using the "@" sign and followed by the name like my @array.
There are many ways to store the data in an array which depends on how user going to use is as shown in below example.
[code]
my @array=(a,b,c,d);
print @array;
[/code]
The above array consists of 4 elements in which array index starts from 0 and ends to maximum declared size which is shown in below image.
In the above image qw() is known as the quote word the signification is used to generate a list of words the user can use the qw in multiple ways as shown in below code.
[perl]
@array1=qw/a b c d/;
@array2= qw' p q r s';
@array3=qw { v x y z};
print @array1;
print @array2;
print @array3;
[/perl]
Result
By running the above code in a Perl command line user can get the following output as shown in below image.
Sequential Array
Description
Sequential array is known as where user store the data sequentially. If user wants to store the numbers from 1-10 or alphabets a-z in an array instead of typing each letter user can use the sequential array as shown in below code.
[perl]
@numbers= (1..10);
print @numbers; #Prints numbers from 1 to 10;
[/perl]
Result
By running the above code in a Perl command line user can get the following output as shown in below image.
Perl Array size
User has the array which is already available but a user doesn't know the array size then the code below demonstrates to find the size of an array as shown.
[perl]
@array= qw/a b c d e/;
print $size=scalar (@array);
[/perl]
Result
By running the above code in a Perl command line user can get the following output as shown in below image.
The above code which always returns the physical size of an array not the number of valid elements. The code below demonstrates the difference between the scalar @array and $#array as shown below.
[perl]
#!/uer/bin/perl
@array = (1,2,3);
$array[50] = 4;
$size = @array;
$max_index = $#array;
print "Size: $size\n";
print "Max Index: $max_index\n";
[/perl]
Result
By running the above code in a Perl command line user can get the following output as shown in below image.
Dynamic Array
Description
Dynamic array is known as which user declaring without specifying any value on them in which user can store the values during the run time. The code below demonstrates the dynamic array.
[perl]
my $string="This is a kind of dynamic array";
my @array;
@array=split('a',$string);
foreach(@array)
{
print "$_ \n"; # This is a special variable which store the current value.
}
[/perl]
Result
By running the above code in a Perl command line user can get the following output as shown in below image.
Push, pop, shift, unshift
These are used to add/delete operations in Perl array elements.
Perl Push
Which is used to add an array element at the end of the existing array.
Perl Pop
Which is used to remove the last element from the array.
Perl Shift
Which is used to remove the first element from an array.
Perl Unshift
Which is used to add an element at the beginning of the array.
The image below demonstrates the Perl Array operations as shown below.
The code below demonstrates the place of using above operations as shown below.
[perl]
@days = ("Mon","Tue","Wed");
print "1st : @days\n";
push(@days, "Thu"); # adds one element at the end of an array
print "2nd when push : @days\n";
unshift(@days, "Fri"); # adds one element at the beginning of an array
print "3rd when unshift : @days\n";
pop(@days);
print "4th when pop : @days\n"; # remove one element from the last of an array.
shift(@days); # remove one element from the beginning of an array.
print "5th when shift : @days\n"
[/perl]
Result
By running the above code in a Perl command line user can get the following output as shown in below image.
Summary
Key Points
Array stores the dat in the form of lists.
Array index starts from 0 and ends to its maximum length.