Lperl Learning Notes-lists and arrays

Source: Internet
Author: User
Tags scalar

Notes from <<perl Language Primer 5th Edition >>

1. An ordered set of indicator quantities in Perl, arrays are variables that store lists, and these terms are often mixed, but more precisely, lists refer to data, and arrays refer to variables. The representation of an array is @my_array because Perl feels that @rray looks like an array, just as $calar looks like scalar scalar variables.

2. Array subscript starting from 0, the array or each element in the list is a separate scalar variable, with a separate scalar value, ordered. Each element has an integer as the subscript index. Because each element is independent of each other, the element types in the list or array can be mixed arbitrarily.

3. The namespaces of the array and the scalar variables are completely separate.

1 #! usr/bin/env Perl2 $roger[0] ="Roger1";3 $roger[1] ="Roger2";4 $roger[3] ="Roger3";5 $roger="Hello";6 Print $roger[1]."\ n";#Roger27 Print $roger."\ n";#Hello

4. Any numeric expression that can be evaluated can be used as subscript, if not an integer, it will automatically drop the decimal, either positive or negative. If the subscript exceeds the end of the array, the corresponding value will be undef; If an element that has an index value that exceeds the end of the array is assigned, the array will grow automatically (without an upper limit), and if additional elements need to be created during the extension, the default value is undef; The index value of the last element is $ #my_array; A negative index is supported, $my _array[$ #my_array] is equivalent to $my _array[-1].

1 #! usr/bin/env Perl2 $roger[0] ="Roger1";3 $roger[1] =undef;4 $roger[2] ="Roger3";5 $roger[3] =undef;6 $end= $#Roger; #表示数组中最后一个元素的索引7 Print '$end ='."$end \ n";#output: $end =38 Print $roger[$end]."\ n";#the Empty

5. List Direct Volume:

You can use the range operator: ; The range operator must require that both ends be integers, otherwise it will automatically be converted to integers, (1..5) <=> (1.7,5.1), noting that the endpoint values have also changed. You can select a subset of the elements of an array to assign a value to it.

1 #! usr/bin/env Perl2 @roger= (1.. -);3 Print$#Roger. " \ n "; #994 @roger= (5.. the);5 Print$#Roger. " \ n "; #106 @roger[0..5] = (5,Ten);7 Print$#Roger. " \ n "; #10

6. You can easily exchange the values of two variables using the list, consider taking out the element values to form a list, and then assigning them two times.

1 #! usr/bin/env Perl2 $roger=1;3 $loger=2;4($roger,$loger) = ($loger,$roger);5 Print $roger."\ n";#26 Print $loger."\ n";#1

7. Multiple array elements and variable values are merged into an array, in which the array name is expanded into a list of elements (which it owns), because the array can contain only scalars and cannot contain other arrays, so the array cannot be an element in the list, and when an array is copied to another array, the assignment operation , only the list is stored in an array. If you put a reference to a list in an array, you can implement the ability to put an array in an array.

1 #! usr/bin/env Perl2 @roger= (1.. -);3 @loger= QW (Hello I am Roger);#QW are equivalent to single quotes, and strings separated by spaces belong to different strings. 4 $Roger=undef;5 @all= (@roger,@loger,undef);6 Print($#all+1); #总的元素数量7 Print "\ n";

8. Push and pop operations for arrays

1 #! usr/bin/env Perl2 @array=5..9;3 Print$#array. " \ n "; #数组原始长度 54 $fred=Pop(@array); 5 Print$#array. " \ n "; #数组当前长度46 Print $fred."\ n";#97 8 @roger=1..Ten;9 Print$#Roger. " \ n "; #数组原始长度10Ten Push(@roger,8); One Print$#Roger. " \ n "; #数组当前长度11 A Print @roger[$#Roger]. " \ n "; #8

If the array is empty, the pop () function returns the Undef;push () function to add one string of elements at a time or another array (to replace the element list when pressed) to the end of the target array. The shift () and unshift () functions operate in the array header, using a similar usage.

1 #! usr/bin/env Perl2 @roger=1..Ten;3 @other=5..Ten;4 Push @roger,@other;#@other Array is pressed into the @roger array5 Print @roger."\ n";

9. Interpolation in the array.

When interpolated, no extra space is added to the end, but separate spaces are automatically added between the elements.

1 #! usr/bin/env Perl2 @rocks=qw{flintstone Slate Rubble};3 Print "Quartz @rocks limestone\n";#interpolation, there will be no extra space. 4 Print @rocks;#There are no spaces between the elements when the output is so5 Print "\ n";6 Print "@rocks \ n";#the output is interpolated and there are spaces between the elements7 Print @rocks."\ n";#so unexpectedly output the number of elements!!! 

Note When inserting messages, because the @ symbol interferes with array recognition

# ! usr/bin/env Perl $email ' [email protected] ' # you can use single quotation marks to assign variables and then interpolate them, but you can also insert them in double quotes. Print " This is my email: $email \ n ";

An index (subscript) expression is treated as a normal string, and the variable in the expression is not interpolated

1 # ! usr/bin/env Perl 2 @roger = (1). Ten ); 3 $index 2 ; 4 Print " elem: @roger [$index + 3]\n " # output Elem:6, it appears that the interpolation is still supported in the new version!!!

10.

Lperl Learning Notes-lists and arrays

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.