Sunday, October 17, 2010

PERL: A hub of magnificient inbuilt functions, Data structures

PERL have amble amount of inbuilt functions and data structures. While we started implementing this structures in C language it will have to take more pain...(HEY not diminishing the C language..PERL itself is written in C). These structures will be helpful for programmers to implement their complex softwares with little effort..

ASSOCIATIVE ARRAYS.....

Associative arrays are a typical data structure which works like a hashing table. Each entry in the associative array have two attributes,: 1)value 2)keys

This typical array can store two values in its first index. This is similar in the case of a student record with student name and roll number. We can retrieve the values as well as the names from the associative array according to our convenience..

Associative array can be declared as "%asso" .The easiest way to create an associative array item is just to assign to it. For example, the statement:

$aasso{"bananas"} = 1;

will assign  associative array " asso" with its first element as "bananas" and the value is 1 . Similarly we can add any number of elements to this associative array. One if the flexibility of this associative array is described below. Suppose we have an array say

"@names=("john",24,"abel",45,"jeas",67)".

We can convert this to an associative array by a simple statement

"%names=@names"

For getting the values from the associative arrays we have two main functions as described above:
They are mainly used for getting the values and attributes in the associative array: Their usage is as follows:

"@nameslist = keys(%names);"

This will returns a list called "namelist". The datas in the name list will not be in sorted order. We can sort this by using the function "sort keys(%names)."

"@valueslist = values(%names);"


This will return the list of the values of the elements in the associative array.


The associative array is the pioneer for other data structures like linked list,tree etc in perl. Also the massive Hash tables can be implemented with less effort by using the associative array....

No comments:

Post a Comment