Perl variables: Array variables

Source: Internet
Author: User
Tags array sort scalar


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/perl

@var_10 = (1..10);  
@var_10 = [1..10];
print @var_10                 #1 2 3 4 5 6 7 8 9 10 print @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
1 Push @ARRAY, LIST

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:
Instance


#!/usr/bin/perl
@sites = qw/11 22 33 44 55 66 77/;
 
@sites2 = @sites[3,4,5];
@sites3 = @sites[3..5];
 
print "@sites2\n";      #44 55 66 print "@sites3\n";            #44 55 66




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


The following example replaces the 5 elements in the array starting with the 6th element:
Instance
#!/usr/bin/perl
 
@nums = (1..10);
Print "Before replacement: @nums\n";
 
Splice(@nums, 5, 5, 21..25);
Print "after replacement: @nums\n";

Execute the above program, the output is:
Before replacement: 1 2 3 4 5 6 7 8 9 10
After replacement: 1 2 3 4 5 21 22 23 24 25 




8. Convert a string to an array
Perl converts a string into an array using the split () function, which has the following syntax:
Split [PATTERN [, EXPR [, LIMIT]]
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
 
# Define string
$var_1 = "hello";
$var_2 = "hello-welcome-world";
$var_3 = "hello,welcome,world,goodbye";
 
# string to an array
@var_1 = split(‘‘, $var_1);
@var_2 = split(‘-‘, $var_2);
@var_3 = split(‘,‘, $var_3);
 
Print "$var_1[1]\n"; #output e
Print "$var_2[1]\n"; #output welcome
Print "$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:
Join EXPR, LIST
Parameter description:
EXPR: Connector.
List: Lists or arrays.


Instance
#!/usr/bin/perl
 
# Define string
$var_1 = "hello-welcome-world";
$var_2 = "hello,welcome,world,goodbye";
 
# string to an array
@var_1 = split(‘-‘, $var_1);
@var_2 = split(‘,‘, $var_2);
 
# Array to a string
$var_1 = join( ‘-‘, @var_1 );
$var_2 = join( ‘,‘, @var_2 );
 
Print "$var_1\n";
Print "$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/perl
 
# Define array
@sites = qw(hello world say goodbye);
Print "Before sorting: @sites\n";
 
# Sort arrays
@sites = sort(@sites);
Print "after sorting: @sites\n";

Execute the above program, the output is:
Before sorting: hello world say goodbye
After sorting: 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/perl
@numbers = (1,3,(4,5,6));
Print "numbers = @numbers\n"; #numbers = 1 3 4 5 6 


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


Instance
#!/usr/bin/perl
 
@odd = (1,3,5);
@even = (2, 4, 6);
 
@numbers = (@odd, @even);
 
Print "numbers = @numbers\n"; #numbers = 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];
The value of print "var is = $var\n" The value of #var is = 1 



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: 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.