Ads

Saturday, December 27, 2008

Introducing perl, Part - II

Perl data types and variables

There are three main types of data and four types of variables supported in perl.

Scalar data and variable: scalar data may be integer literals written without a decimal point or an ‘E’ exponent (e.g; 1332, 0x33), floating point literals numbers written with a single decimal point and or an ‘E’ exponent (e.g; 12.3, 3E7), or character string literals that are enclosed in single or double quotes (e.g; “Hello World”). A variable, which holds scalar data, is called a scalar variable. Its name always begins with ‘$’, e.g;

$varint=13, $varfloat=12.3, $varstring=”youth adda”


Array of scalar and array variables: An array is an ordered collection of scalar quantities. It can contain multiple scalars or none at all. Any element of the array can be accessed by an integer index or subscript. Normally, an array containing ‘n’ elements has indices starting at 0 and going to n-1, inclusive (1, 2, 3), (‘a’, ’b’, ‘c’), ( ) are some example of array of scalars. Any variable holding an array of scalars is called an array variable and its name should start with ‘@’, e.g; @vararray=(1, 2, 3). There exists a scalar variable @#arrayname, where arrayname is the name of the array for every array that contains a subscript of the last element of the array.

Associative array of scalars and associative array: An associative array contains any number of elements that may be indexed by arbitrary character strings, e.g; (“color”, “black”, “red” 6.8). You might think that associative array elements look the same as normal array elements. But there is a difference. In associative arrays, a successive pair represents the index or key and the value respectively. An associative array is a power feature of perl. Any variable holding an associative array of scalars is called associative array variable, and its name should start with ‘%’, e.g; @varassocarray = (“color”, “black”, “red” 5.6). The key () function can be used to get the array of all keys related to an associated array.

There is a significant difference between using the single quote and the double quote to delimit strings. If a string is delimited by a pair of single quote, then all characters within the string are taken literally, and there will be no substitution. If the string is delimited by double-quotes, a number of escape sequences will be allowed, and perl will substitute the value of the variables appearing in the string.

We will take a look at the expressions of perl in third part.
Enjoy learning!!

No comments: