* Lists, which means ordered collections of scalars, are often used as if it is interchangeable with arrays, while the latter are actually the data structure, which, worthy enough to be pointed out to those C programmers, are considered as variables.
* Numbers, strings and undef values can be held in a same array.
* The syntax for refering an array is
@array_names
while the way to access an elements is usually
$array_names[index]
* The array is automatically extended as needed. The following code
$a_array[0] = 'the value';
$a_array[100] = 'the end value';
creates 99 array elements whose value is undef.
* A list literal is a list of comma-separated values enclosed with parentheses. ".." is a range operator. It creates a list of values by counting from the left scalar, which must be smaller than the latter, up to the right ones.
* A great shortcut to generate a list of simple words is to use qw. Which saves the quotes marks and commas. Whitespaces between words are discarded. Any punctuation character can be used as the delimiter instead of parentheses. Put a backslash before the delimiter when it appears in the list.
* List assignment is applied by using, not surprisingly, the equal character.
list_one = list_two.
The list can be either arrays or list literals.
* Using arrays as stacks, pop and push operator works on the last elements of an array.
$scalar = pop @an_array; # take the last elements off and return it.
push @an_array,@another_array; # add the second array(which can also be a scalar) to the end of the first one.
* shift operator takes off the first element of an array and return its value. unshift do the opposite thing.
* Arrays may be interpolated into space-separated string in a double quoted string.
* Syntax of foreach control structure:
foreach $iterate_scalar ( iterated_list ) {
code block;
}
When the iterate scalar is omitted, the default $_ will be there instead.
* The reverse operator reverses the order the elements in a list.
* The sort operator sorts the elements of a list into dictionary order
* reverse operator generates a scalar when used in scalar context.
* A scalar is automatically promoted to make a one-element list in list context.
* to force a scalar context: put scalar before the array.
*
Tuesday, February 5, 2008
Learning Perl, 4th Edition Notes 0x2
Learning Perl, 4th Edition Notes 0x2 by DaNmarner
Labels:
Notes,
Perl,
Programming,
Script
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment