top of page

Perl Array

 

   An Array is a special type of variable which stores data in the form of a list; each element can be accessed using the index number which will be unique for each and every element. You can store numbers, strings, floating values, etc. in your array. This looks great, So how do we create an array in Perl? In Perl, you can define an array using '@' character followed by the name that you want to give. Let's consider defining an array in Perl.

my @array;

This is how we define an array in Perl; you might be thinking how we need to store data in it. There are different ways of storing data in an array. This depends on how you are going to use it.

my @abcd=(ant,book,cat,dog);
print @abcd;
antbookcatdog


Sequential Array

Sequential arrays are those where you store data sequentially. Suppose, you want to store 1-10 numbers or alphabets a-z in an array. Instead of typing all the letters

@numbers= (1..100);
print @numbers;
@alpha= (a..z);
print @alpha;

Perl Array Size

We have an array which is already available, and you don't know what the size of that array is, so what is the possible way to find that.

@abcd=(ant,book,cat,dog);
print $size=scalar (@abcd);

 

Push, Pop, shift, unshift for Perl arrays:

These functions can be used in Perl to add/delete to array elements.

Perl Push: adds array element at the end of an existing array.
Perl Pop: removes the last element from an array.
Perl Shift: removes the first element from an array.
Perl Unshift: adds an element at the beginning of an array.
 

# create a simple array
@names = ("Thiru","Raja","Selvi");
print "1. \@names  = @names\n";

# add one element at the end of the array
push(@names, "Bala");
print "2. \@names  = @names\n";

# add one element at the beginning of the array
unshift(@names, "Bhuvana");
print "3. \@names  = @names\n";

# remove one element from the last of the array.
pop(@names);
print "4. \@names  = @names\n";

# remove one element from the beginning of the array.
shift(@names);
print "5. \@names  = @names\n";


Array Slicing


To extract a "slice" from an array

@days = (Mon,Tue,Wed,Thu,Fri,Sat,Sun);
@weekends = @days[5,6];
print "@weekends\n";


Replacing Array Elements

splice @ARRAY, OFFSET [ , LENGTH [ , LIST ] ]

#!/usr/bin/perl

@nums = (1..30);
print "nonreplaced - @nums\n";

splice(@nums, 5, 10, a..z); 
print "replaced - @nums\n";

nonreplaced - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30                                        replaced - 1 2 3 4 5 a b c d e f g h i j k l m n o p q r s t u v w x y z 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

 

split()


$var_string = "we-are-back-on-the-show";
$var_names = "a,b,c,d,e";

# transform above strings into arrays.
@string = split('-', $var_string);
@names  = split(',', $var_names);

print "$string[3]\n";  
print "$names[4]\n";

on                                                                                                                                                                                                                       e 

 

sort()


@letters = qw(p s c b);
print "Before sort: @letters\n";

# sort this array
@letters = sort(@letters);
print "After sort: @letters\n";

Before sort: p s c b
After sort: b c p s 


@constants = (B, C, D, F, G, H, I, J, K, L, M, N, P, Q, R, S, T, V, W, X, Y, Z);
@vowels = (A, E, I, O, U);

@alphabets = (@constants, @vowels);

print "alphabets = @alphabets\n";

alphabets = B C D F G H I J K L M N P Q R S T V W X Y Z A E I O U

@list = (1,2,3,4,5)[1..2];

print "Value of list = @list\n";
Value of list = 2 3

 

© 2015 Thirumurugan

bottom of page