Learning notes for Perl-Chapter 3 1
This article mainly introduces some arrays and lists, allowing you to fully understand the convenience and strength of Perl :)
Array:
1. The array is counted from 0.
2. Calculate the index value of the last element: $ # Rocks
3. Simply calculate the value of the last number: rocks [-1]
The range operator is used to indicate the direct quantity of the list:
(1. 5): 1, 2, 3, 4, 5
(5.7 ..): indicates that 1, 2, 3, 4, 5 decimal places are removed.
(): Empty list, only online count allowed
QW:
QW/Fred Barney Betty Wilma Dino /;
QW {
/Usr/local/words
/Home/rootbeer/. ispell_english
}
The QW operator can be used to easily define the list, and the front and back operators can be defined by themselves.
List assignment:
($ Fred, $ Barney, $ Dino) = ("Flintstone", "rubble", UNDEF );
Variable transformation method:
($ Fred, $ Barney) = ($ Barney, $ Fred );
Array assignment:
@ Rocks = QW/bedrock slate lava /;
Array replication:
@ Rocks = @ copy
Pop and push operators:
The pop function extracts the last element of an array and returns it:
@ Array = 5 .. 9;
$ Fred = POP (@ array); # $ Fred Get 9, @ array now has)
$ Fred = pop @ array; # $ Fred gets 8. Don't use the same brackets. @ array now has (, 6, 7)
The push function adds an element at the end of the array.
Push (@ array, 0); # Now @ array has (5, 6, 7, 0)
Push @ array, 8; # Now @ array has (5, 6, 7, 0, 8)
Push @ array, 1 .. 10; # @ array with 10 new elements added
Shift and unshift functions:
Operate on the beginning of the array, similar to pop and push;
@ Array = QW # Dino Fred Barney #;
$ A = shift (@ array); # $ A get Dino, @ array now ("Fred", "Barney ");
Unshift @ array, 4; # @ array now has 4, Fred, barney