Perl variables: Array variables

Source: Internet
Author: User

Tags: Table variable split perl out offset array element ENC brackets EXP

Perl Arrays
a Perl array is a list variable that stores scalar values, and variables can be of different types.
array variables begin with @. Access array elements are read using the $ + variable name + [index value] format.

1. Creating an array
1. The array variable starts with the @ symbol, and the element is enclosed in parentheses.
2. Begin defining the array as QW, the second array uses the qw//operator, which returns a list of strings, and the array elements are separated by a space.
@array1 = (1, 2, ' Hello ');
@array2 = [1, 2, ' Hello '];
@array3 = Qw/this is a array/;

print @array1 [0]; #1
print @array1; #12hello

print @array2 [0]; #1
print @array2; #ARRAY (0x26e3160)

2. Accessing array elements
Access array elements are read using the $ + variable name + [index value] format.
array index value
read from the forward direction, starting with 0, that is, 0 is the first element, 1 is the second element, and so on.
negative numbers are read from the reverse, starting at 1, that is, 1 is the first element, and 2 is the second element.

3. Array serial number
Perl provides an array form that can be output in sequence, in the format (start value: End value).

#!/usr/bin/= (1.. Ten );   = [1.. Ten ];p rint @var_10                 #1234567  89print @var_10                 #ARRAY (0x26e3160

4. Array size
The size of the array is determined by the scalar context in the array.


5. Adding and removing array functions
Perl provides some useful functions to add and remove array elements.

Serial Number Type and Description

Place the value of the list at the end of the array

2 Pop @ARRAY

pops up the last value of the array and returns it

3 Shift @ARRAY

POPs the first value of the array and returns it. The index value of the array is also reduced by one.

4 Unshift @ARRAY, LIST

Puts the list in front of the array and returns the number of elements in the new array.

6. Cut Array
We can cut an array and return the new array after the cut:

#!/usr/bin/perl@sites= qw/ One  A  -  -  -  the  the/; @sites2= @sites [3,4,5]; @sites3= @sites [3..5]; Print"@sites2 \ n"; # -  -  thePrint"@sites3 \ n"; # -  -  the

7. Replacing array elements
The array element substitution in Perl uses the splice () function, which has the following syntax:
Parameter description:
@ARRAY: the array to replace.
OFFSET: The starting position.
LENGTH: The number of elements to replace.
List: replace element lists.

The following instance replaces the 5 elements in the array starting with the 6th element: instance #!/usr/bin/Perl @nums= (1..Ten);p rint"before replacement: @nums \ n"; Splice (@nums,5,5, +.. -);p rint"after replacement: @nums \ n"; Execute above procedure, output result: Before replace:1 2 3 4 5 6 7 8 9 Tenafter replacement:1 2 3 4 5  +  A  at  -  - 

8. Convert a string to an array
Perl converts a string into an array using the split () function, which has the following syntax:
Parameter description:
PATTERN: Delimiter, the default is a space.
EXPR: Specifies the number of strings.
LIMIT: If this parameter is specified, the number of elements of the array is returned.

Example: #!/usr/bin/perl # definition string $var_1="Hello"; $var _2="Hello-welcome-world"; $var _3="Hello,welcome,world,goodbye"; # string to array @var_1= Split ("', $var _1); @var_2= Split ('-', $var _2); @var_3= Split (',', $var _3); Print"$var _1[1]\n"; # Output Eprint"$var _2[1]\n"; # Output Welcomeprint"$var _3[1]\n"; # Output Welcome

9. Converting an array to a string
In Perl, convert an array to a string using the Join () function, which has the following syntax:
Parameter description:
EXPR: Connector.
List: Lists or arrays.

Instance #!/usr/bin/perl # definition string $var_1="Hello-welcome-world"; $var _2="Hello,welcome,world,goodbye"; # string to array @var_1= Split ('-', $var _1); @var_2= Split (',', $var _2); # Array to string $var_1= Join ('-', @var_1); $var _2= Join (',', @var_2); Print"$var _1\n";p rint"$var _2\n";

10, array sort
perl array sorting using the sort () function, the syntax format is as follows:
sort [subroutine] LIST
parameter description:
    subroutine: Specifies the rule.
    list: Lists or arrays.

Instance #!/usr/bin/=" before sorting: @sites \ n"="  after sorting: @sites \ n"; Execute the above program, output: Before sorting: Hello World Say goodbye sorted: Hello Goodbye say world 

11. Merging arrays
The elements of the array are separated by commas, and we can use commas to merge the arrays as follows:

Instance #!/usr/bin/= (1,3, (4,5,6" numbers = @numbers \ n ";     1 3 4 5 6

You can also embed multiple arrays in the array and merge them into the main array:

Instance #!/usr/bin/= (1,3,5= (246  ="numbers = @numbers \ n";   1 3 5 2 4 6

12. Select an element from the list
A list can be used as an array, and the specified index value after the list can read the specified element as follows:

Instance #!/usr/bin/perl $var = (5,4,3,2,1) [ 4  "var value = $var \ n"     #var1

Perl variables: Array variables

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: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: