Perl Programming - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Perl Array

Perl Array

shape Introduction

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

shape 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

shape 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

shape 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 "$_ &#92n"; # 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. 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&#92n"; push(@days, "Thu"); # adds one element at the end of an array print "2nd when push : @days&#92n"; unshift(@days, "Fri"); # adds one element at the beginning of an array print "3rd when unshift : @days&#92n"; pop(@days); print "4th when pop : @days&#92n"; # 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&#92n" [/perl] Result By running the above code in a Perl command line user can get the following output as shown in below image.

Summary

shape Key Points

  • Array stores the dat in the form of lists.
  • Array index starts from 0 and ends to its maximum length.
  • qw means quote word.