A summary of the Perl Language Primer-3rd chapter-List and array
Source: Internet
Author: User
1-list list refers to an ordered collection of titles, while array is a variable that stores a list.
More precisely, lists refer to data and arrays refer to variables.
Accessing elements in an array
$ fred [0] = "yabba";
$ fred [1] = "dabba";
$ fred [2] = "doo";
$ number = 2.71111;
print $ fred [$ number-1]; #dabba If the subscript is not an integer, the decimal will be truncated.
print $ fred [145] #If no subscript exists, return undef
Special array index to get the last element
$ rocks [0] = "one";
$ rock [99] = ‘last’;
print $ rock [$ # rock]; #last
print $ rock [-1]; #last
List direct
(1,2,3) (1,2,3,) ("friend", 11) () (1..100)
(1.7..5.7) (5..1) empty list (0,2..6,10,12)
("fred", "barrbey" m "wilma")
qw abbreviation
qw (fred barrbey wilma)
qw allows the use of any punctuation as a delimiter.
qw / fred barney betty / # can use backslash \ escape definition
List assignment
($ fred, $ barney, $ dino) = (‘a’, ‘b’, ‘c’);
print $ barney; #result is b
($ a, $ b) = ($ b, $ a); # a, b exchange of two variable values
Array variable assignment and shorthand
($ rocks [0], $ rocks [1], $ rocks [2]) = qw / a b c /;
@rocks = qw / a b c /; #shorthand for the above method
Array operations
@a = qw / a b /;
@b = (@ a, "c", ‘d’); # a, b, c, d
pop and push operations, the rightmost operation
@array = 5..8;
$ array1 = pop (@array); #will remove the last element 8 from the array and return 8
push @array, 100; #Add 100 backward
shift and unshift operators, which operate on the far left
@array = qw # a b c d #;
$ m = shift (@array); # @ arrayvalues are (b, c, d)
unshift (@array, 100); #Add 100 forward (that is, (100, b, c, d)
Perl language introduction-chapter 3-lists and arrays
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.