Perl-07-array

Source: Internet
Author: User

1. Length of the array:

If you assign an array variable to a scalar variable, the length of the array is assigned to the scalar variable:

$ A = @ array; # The length of the array @ array is stored in the scalar variable $ A, that is, the number of elements in the array;

($ A, $ B, $ c) = (, 3); # assign 1 to $ A, assign 2 to $ B, and assign 3 to $ C;

The index of the subscript of the last element in the array is determined by the special variable $ # arrayname of the array. It is represented by a special variable with "$ # + array name, this variable stores the subscript index value of the last storage unit in the array; $ # arrayname, also known as the index value of the last unit, always indicates the bottom value of the last storage unit in the array;

Therefore, you can use the special variable "$ # array name" to calculate the number of elements in the array:

# Arraylength =$ # arrayname + 1;

The first storage unit corresponds to the last storage unit in the array. in Perl, the special variable "$ [" of the array is used to represent the lower value of the first storage unit in the array, that is: index value of the first storage unit in the array. You can modify the value of this special variable to change the index value of the first storage unit in the array, the subscript index value of the storage unit that is expected to be obtained by adding 1 to the value of the special variable "$;

The special variable "$ # + array name" in the array indicates the subscript index value of the last storage unit in the array, in Perl, you can change the value of this special variable to add or delete elements in the array;

For example, if you want to add n new elements at the end of the array @ array,

You can do this: $ # array + = N; or @ array [$ # array + 1] = value;

This indicates that you can use the special variable "$ # + array name" to set the length of the array. Perl allows you to add new array units at any time in the array. In this case, the system automatically expands the storage space and length of the array when the new array unit is added. Therefore, arrays in Perl are dynamic arrays, which are convenient for processing small arrays. However, when the amount of data to be processed is large, the program running speed is greatly slowed down. Every time a new array unit is added, the system automatically expands the space and length of the array, and the expansion operation takes time, so the program slows down; if you know the length of the array to be processed in advance, you can set the special variable "$ # + array name" to initialize the storage unit space of the array, and then use the array, this will improve the data processing efficiency;

2. add and delete elements in the array:

You can add new elements to the array by adding the value of the special variable "$ # + array name". Perl will automatically expand the storage space of the array:

$ Array [$ # array ++] = $ newvar;

You can delete the elements in the array by reducing the value of the special variable "$ # + array name". Perl automatically recycles the storage space of the array:

$ # Array ++; # Delete the last element

$ # Array + = N; # Delete the last n elements

To set the array to an empty array, you only need to set "$ # + array name" to-1: $ # array =-1;

In Perl, the default array unit subscript number starts from 0 and increments sequentially. However, if the program has special requirements, you can change the starting value of the subscript index of an array storage unit by changing the value of the special variable "$. $ [This parameter can be set to any number, and the remaining numbers are incremented based on this value;

3. Read array data:

Read the value of an element: $ A = $ array [$ Index]; or $ A = @ array [$ Index];

Read the value of some elements: @ subarray = @ array [$ startindex... $ endindex]; # this is also a method for reading sub-arrays;

Note: when accessing the elements in the array @ array, if the subscript $ Index given exceeds the subscript range of the array @ array, you need to access

If the element does not exist, an undefined value or null value (None/null) is returned );

4. subarray:

When reading array element values, you can read some elements in the array as another new array. This new array is called a subarray or an array block; the rules for sub-arrays are the same as those for arrays;

The sub-array is defined by the characters "@" and "[]": @ subarray = @ parentarrayname [startindex .. endindex];

When referencing elements in an array, You can reference discontinuous elements in the array, or reference array elements in sequence or assign values to array elements;

Related Article

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.