home PYTHONJAVA
 

Perl array

Perl arrays are list variables that store scalar values, which can be of different types.

Array variables start with @. Accessing array elements is done using the $ + variable name + [index value] format, as shown in the following example:

Instance

#!/usr/bin/perl @hits = ( 25, 30, 40) ; @names= ( "google", "welookups ", "javacodegeeks "); print "\$hits[0] = $hits[0]\n"; print "\$hits[1] = $hits[1]\n"; print "\$hits[2] = $hits[2]\n"; print "\$names[0] = $names[0]\n"; print "\$names[1] = $names[1]\n"; print "\$names[2] = $names[2]\n";

The $ symbol in the program uses \ to escape and let him output as it is.

Execute the above program, the output is:

$hits[0] = /span>25
$hits[1] = 30
$hits[2] = 40
$names[0] = google
$names[1] = welookups
$names[2] = javacodegeeks

Create an array

Array variables start with the @ symbol, the element is enclosed in parentheses, and the array can be defined starting with qw.

@array = (1, 2, 'Hello');
@array = qw/This is one array/;

The second array uses the qw// operator, which returns a list of strings separated by spaces. Of course, you can also use multiple rows to define an array:

@days = qw/google
javacodegeeks
...
welookups/;

You can also assign values ​​to an array by index, as shown below:

$array[0] = /span>'Monday';
...
$array[6] = 'Sunday';

Access array elements

Accessing array elements is done using the $ + variable name + [index value] format, as shown in the following example:

Instance

@sites = qw/google javacodegeeks welookups/; print "$sites[0]\n"; print "$sites[1]\n"; print "$sites[2]\n"; print "$sites[-1]\n"; # Negative, reverse reading

Execute the above program, the output is:

google
javacodegeeks
welookups
welookups

Array index values ​​start at 0, where 0 is the first element, 1 is the second element, and so on.

Negative numbers are read from the reverse, with -1 being the first element and -2 being the second element


Array serial number

Perl provides an array of output that can be output in a sequence of starting value + .. + ending value, as shown in the following example:

Instance

#!/usr/bin/perl @var_10 = (1..10); @var_20 = (10..20); @var_abc = ( a..z); print "@var_10\n"; # Output 1 to 10 print "@var_20\n"; # Output 10 to 20 print "@var_abc\n"; #output a to z

Execute the above program, the output is:

1 2 3 4 5 6 7 8 9 10
10 11 12 13 14 15 16 17 18 19 20
a b c d e f g h i j k l m n o p q r s t u v w x y z

Array size

The array size is determined by the scalar context in the array. :

@array = (1,2,3 );
print "Array size: ", scalar @array,"\n";

The length of the array returns the physical size of the array, not the number of elements. We can see the following example:

Instance

#!/uer/bin/perl @array = ( 1,2,3) ; $array[50 ] = 4; $size = @array; $max_index = $#array; print "Array size: $size\n"; print "Maximum index: $max_index\n";

Execute the above program, the output is:

array size: 51
Max Index: 50

As you can see from the output, there are only four array elements, but the array size is 51.


Add and remove array elements

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

If you don't have programming experience before, you might ask what is a function. In fact, the print we used before is an output function.

The following table lists the commonly used operation functions in arrays:

Order Type and Description
1 push @ARRAY, LIST

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

2 pop @ARRAY

Pops 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 decremented by one.

4 unshift @ARRAY, LIST

Place the list in front of the array and return the number of elements in the new array.

Instance

#!/usr/bin/perl # Create a simple array @sites = ( "google","welookups ","javacodegeeks "); print "1. \@sites = @sites\n"; # Add an element to the end of the array push(@sites , "baidu"); print "2. \@sites = @sites\n"; # Add an element at the beginning of the array unshift(@sites , "weibo"); print "3. \@sites = @sites\n"; # Remove elements at the end of the array pop(@sites ); print "4. \@sites = @sites\n"; # Remove elements at the beginning of the array shift(@sites ); print "5. \@sites = @sites\n";

Execute the above program, the output is:


cut array

We can cut an array and return the new array after cutting:

Instance

#!/usr/bin/perl @sites = qw/google javacodegeeks welookups weibo qq facebook Netease/; @sites2 = @sites[3,4,5]; print "@sites2\n";

Execute the above program, the output is:

weibo qq facebook

Array indexes need to specify a valid index value, which can be negative after a positive number, and each index value is separated by a comma.

If it is a contiguous index, you can use .. to indicate the specified range:

Instance

#!/usr/bin/perl @sites = qw/google javacodegeeks welookups weibo qq facebook Netease/; @sites2 = @sites[3..5]; print "@sites2\n";

Execute the above program, the output is:

weibo qq facebook

Replace array elements

The array element substitution in Perl uses the splice() function with the following syntax:

splice @ARRAY, OFFSET [ ,  LENGTH [  , LIST ] ]

Parameter description:

  • @ARRAY: The array to replace.
  • OFFSET: Starting position.
  • LENGTH: The number of elements to replace.
  • LIST: Replace the list of elements.

The following example replaces the 5 elements in the array starting with the 6th element:

Instance

#!/usr/bin/perl @nums = ( 1..20); print "Before replacement - @nums\n"; splice(@nums , 5, 5, 21 ..25); print "After replacement - @nums\n";

Execute the above program, the output is:

Before replacing - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
After replacement - 1 2 3 4 5 21 22 23 24 25 11 12 13 14 15 16 17 18 19 20

Convert a string to an array

Converting a string to an array in Perl uses the split() function with the following syntax:

split [ PATTERN [ , EXPR [ , LIMIT ] ] ]

Parameter description:

  • PATTERN: separator, defaults to a space.
  • EXPR: Specify the number of strings.
  • LIMIT: If this parameter is specified, the number of elements in the array is returned.

Instance

#!/usr/bin/perl #Defining strings $var_test= " welookups"; $var_string= " www-welookups-com"; $var_names = " google,javacodegeeks,welookups,weibo"; # string to array @test = split ('', $var_test); @string = split ('-', $var_string); @names= split (',', $var_names); print "$test[3]\n"; # output o print "$string[2]\n"; # output com print "$names[3]\n"; # export weibo

Execute the above program, the output is:

o
Com
Weibo

Convert an array to a string

Converting an array to a string in Perl uses the join() function with the following syntax:

join EXPR, LIST

Parameter description:

  • EXPR: Connector.
  • LIST: list or array.

Instance

#!/usr/bin/perl #Defining strings $var_string= " www-welookups-com"; $var_names = " google,javacodegeeks,welookups,weibo"; # string to array @string = split ('-', $var_string); @names= split (',', $var_names); # Array to String $string1= join ( '-', @ String ); $string2 = join ( ',', @ Names ); print "$string1\n"; print "$string2\n";

Execute the above program, the output is:

www-welookups-com
Google,javacodegeeks,welookups,weibo

Array sorting

Array sorting in Perl uses the sort() function with the following syntax:

sort [ SUBROUTINE ] LIST

Parameter description:

  • SUBROUTINE: Specify the rule.
  • LIST: list or array.

Instance

#!/usr/bin/perl #Defining an array @sites = qw(google javacodegeeks welookups facebook); print "Before sorting: @sites\n"; # Sort arrays @sites = sort (@sites); print "After sorting: @sites\n";

Execute the above program, the output is:

Before sorting: google javacodegeeks welookups facebook
After sorting: facebook google welookups javacodegeeks

Note: Array sorting is based on ASCII numeric values. So when we sort the array, we should first convert each element to lowercase and then sort.


Special variable: $[

The special variable $[ represents the first index value of the array, which is generally 0. If we set $[ to 1, the first index of the array The value is 1, the second is 2, and so on. An example is as follows:

Instance

#!/usr/bin/perl #Defining an array @sites = qw(google javacodegeeks welookups facebook); print "Website: @sites\n"; # Set the first index of the array to 1 $[= 1 ; print "\@sites[1]: $sites[1]\n"; print "\@sites[2]: $sites[2]\n";

Execute the above program, the output is:

Website: google javacodegeeks welookups facebook
@sites[1]: google
@sites[2]: javacodegeeks

Generally, we don't recommend using the special variable $[, which has been deprecated in the new version of Perl.


Merge arrays

Elements of arrays are separated by commas, and we can also use commas to merge arrays as follows:






welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.