Saturday, December 27, 2014

Tutorial 4 : Arrays Example

In this tutorial, we will show you how to perform arrays data type in Perl language. Beside that, we also will show you what is the special function can be done by it.

Arrays

Variable Arrays

Arrays is a variable which can store an ordered list of scalar values. In Perl language, the arrays variables come before by an "at" (@) sign. When we want to refer to a single element of the array, we can use the dollar sign ($) with the variable name and followed by the index of the element in square brackets ([index]). Below is the example to show you how to use an arrays variable.

#!/usr/bin/perl

@month = ("January","February","March");
@day = (1,2,3,4,5,6,7);

print "Today is $day[0]st $month[0] 2014\n";
print "My friend's birthday on $day[2]rd of $month[2] 1990\n";

The output of this example is shown as video below:



Arrays Elements

In arrays, the first element is started with index 0, then followed by increasing number. When accessing individual elements in the array, we must prefix the variable with dollar sign ($) and follow by the element index after the name of variable. Below is the example of this part.

#!/usr/bin/perl

@month = ("January", "March", "May", "July", "Jun", "December");

print "\@month[0] = @month[0]\n";
print "\@month[1] = @month[1]\n";
print "\@month[2] = @month[2]\n";
print "\@month[3] = @month[3]\n";
print "\@month[4] = @month[4]\n";
print "\@month[5] = @month[5]\n";
print "\@month[-1] = @month[-1]\n";
print "\@month[-2] = @month[-2]\n";
print "\@month[-3] = @month[-3]\n";
print "\@month[-4] = @month[-4]\n";

In normal condition, the array indices start from zero, thus to access first element of the array, we need to indicate 0 as the indices. There is a special condition as well, when we are given a negative index to the array, it will select the element form the end  instead of the beginning of the array. This mean that, @month[5] = @month[-1], @month[4] = @month[-2], ... at the example above. The video below shows the expected output of this example.


Sequential Arrays & Array Size

If you wish to create an arrays which is from 0 to 100, it is troublesome for you to type out all of them. Thus, there is an easy way which is offered by Perl. We can use a shortcut for create a sequential numbers or letters arrays. Besides that, Perl also provide us a way to return the value of the size of an array and the last index in an arrays. Below is the example of this part:

 #!/usr/bin/perl

@number = (0..20);          # include the number from 0 to 20
@alpha = (d..r);            # include the character from d to r
$size = @number;            # calculate the size of array of number
$last_index = $#number;     # save the last index of the arrays

print "@number\n";
print "@alpha\n";
print "The size of the arrays of number : $size\n";
print "The last index of the arrays : $last_index\n";

The output of this example is shown as video below:


Adding and Removing Elements in Array

In Perl, we can add and remove the element into an array. There are four commands we can use and they have different kind of function. Push command will add-on a new element into the array at last index; Unshift command will add-on a new element into the array at first index; Pop command will remove the last index from the array; Shift command will remove the first index from the arrays. Below is the example how we use these commands to perform some operations in Perl language.

#!/usr/bin/perl

@name = ("Ali", "Abu", "Ah Meng");
print "Original : @name\n";

push (@name, "Ah Keong");           # Add in Ah Keong in array as last index
print "Add as last index : @name\n";

unshift (@name, "Ah Mei");          # Add in Ah Mei in array as first index
print "Add as first index : @name\n";

pop (@name);                        # Remove the last index in the array
print "Remove the last index : @name\n";

shift (@name);                      # Remove the first index in the array
print "Remove the first index : @name\n";

The output of this example is shown as the video below:




Slicing and Replacing Array Elements

In this part, we will show you how to slice a list of number from the original array. Besides that, we also can replace the particular elements with other set of array by using splice commands. The example code is shown as below:

#!/usr/bin/perl

@number = (1..20);
@slice_number = @number[3..10];    # slice the number from 3 to 10

print "@slice_number\n";

splice (@number, 3, 6, 30..35);    # slice the number and replace with others
print "@number\n";


The splice command syntax is splice (@array, offset, length, @replacing_array). The output of above example is shown as below.




Split and Join Arrays

In this part, we will learn how to split a long string which have a fix pattern such as "," or "-" or " " into a list of array. On the other hands, we will learn how to join a number of elements to become a long string.

The Perl command that we will use in this part is split and join. The syntax is shown as here: split ('pattern', $variable) and join ('pattern', @array). Let's see how we can use these commands to perform some tasks.

#!/usr/bin/perl

$name = "Abu-Ali-Akong-Amei-Ane";
$date = "1,2,3,4,5,6,7,8,9,10";

@name = split ('-',$name);
@date = split(',', $date);

print "@name\n";
print "@date\n";

$new_name = join (';',@name);
$new_date = join ('=',@date);

print "$new_name\n";
print "$new_date\n";

The output of this example is shown as video below.



Sort and Merging Array

In Perl, we can rearrange the elements in an arrays in ascending order by simply using sort command. Besides, we also can merge two arrays to become a single array. Let's us see how it works.

#!/usr/bin/perl

@days = ("Monday","Tuesday","Wednesday","Thursday","Friday");
@month = ("Jan","Feb","Mar","Apr","May","Jun");

@merge = (@days, @month);       # merge two arrays to be one
print "@merge\n";

@arrange = sort(@merge);      # rearrange all the elements in ascending order
print "@arrange\n";

The output is shown as the video at below.


Hopefully, all of you have fun with my tutorial. In next tutorial, we will learn another data type in Perl language. Be stay tuned for my tutorial update. :D

No comments:

Post a Comment