Perl array


Release date:2023-10-12 Update date:2023-10-21 Editor:admin View counts:342

Label:

Perl array

Perl array is a list variable that stores scalar values, and variables can be of different types.

Array variables to @ beginning. Access array elements using the $ + Variable name+[index value] format. Examples are as follows:

Example

#!/usr/bin/perl@hits=(25,30,40);@names=("google","runoob","taobao");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 usesto escape and let him output as is.

Execute the above program, and the output is as follows:

$hits[0] = 25
$hits[1] = 30
$hits[2] = 40
$names[0] = google
$names[1] = runoob
$names[2] = taobao

Create an array

Array variables to @ start with the symbol, and the element is placed in parentheses, or you can start with qw start defining the array.

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

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

@days = qw/google
taobao
...
runoob/;

You can also assign values to the array by index, as follows:

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

Access array elements

Access array elements using the $ +Variable name+[index value] format. Examples are as follows:

Example

@sites=qw/google taobao
runoob/;print"$sites[0]\\n";print"$sites[1]\\n";print"$sites[2]\\n";print"$sites[-1]\\n";#
Negative number, reverse read

Execute the above program, and the output is as follows:

google
taobao
runoob
runoob

The array index value starts at 0, that is, 0 is the first element, 1 is thesecond element, and so on.

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

Array serial number

Perl provides an array form that can be output by sequence, in the format of the starting value + .. + end value. An example is as follows:

Example

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

Execute the above program, and the output is as follows:

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:

Example

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

Execute the above program, and the output is as follows:

Array Size: 3

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

Example

#!/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, and the output is as follows:

Array size: 51
Maximum 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 have no previous programming experience, you may ask what a function is, but what we used before print that is, an output function.

The following table lists the common operation functions in the array:

Serial number

Type and description

1

Push @ ARRAY, LIST puts the values of the list at the end of the array

2

Pop @ ARRAY deletes the last value of the array

3

Shift @ ARRAY pops up the first value of the array and returns it. The indexvalue of the array also decreases by one in turn.

4

Unshift @ ARRAY, LIST puts the list in front of the array and returns the number of elements of the new array.

Example

#/ Usr/bin/perl#
Create a simple array @ sites=("Google", "runoob", "taobao")$ New_ Size=@ sites; Print "1. @ sites
=@ sites n "." Original array length: $new_ Size n "#
Add an element $new to the end of the array_ Size=push (@ sites, "baidu"); Print "2. @ sites=
@Sites n "." New array length: $new_ Size n "#
Add an element $new at the beginning of the array_ Size=unshift (@ sites, "weibo"); Print "3. @ sites
=@ sites n "." New array length: $new_ Size n "#
Delete element $new at the end of the array_ Byte=pop (@ sites); Print "4. @ sites=
@The pop-up element for sites n "." is: $new_ Byte n "#
Remove element $new at the beginning of the array_ Byte=shift (@ sites); Print "5. @ sites=
@The pop-up element for sites n "." is: $new_ Byte n ";

Execute the above program, and the output is as follows:

Image0

Cut array

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

Example

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

Execute the above program, and the output is as follows:

weibo qq facebook

The array index needs to specify valid index values, which can be positive followed by negative numbers, each separated by a comma.

If it is a continuous index, you can use the .. to represent the specified range:

Example

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

Execute the above program, and the output is as follows:

weibo qq facebook

Replace array elements

Perl substitution of array elements in the splice() function, the syntax format is as follows:

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 five elements in the array starting with the sixth element:

Example

#!/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, and the output is as follows:

Before replacement - 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

Perl to convert a string to an array in the split() function, the syntax format is as follows:

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

Parameter description:

  • PATTERN : Delimiter, which defaults to 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_ Test="runoob"$ Var_ String="www runoob com"$ Var_ Names="Google, Taobao, runoob, Weibo"#
Convert the string to an array @ test=split ('', $var_test)@ String=split ('-', $var_string)@ Names=split
(',', $var_names); Print "$test [3] n"#
Output oprint "$string [2] n"# Output compile '$names [3] n'# Output Weibo

Execute the above program, and the output is as follows:

o
com
weibo

Convert an array to a string

Perl to convert an array to a string in the join() function, the syntax format is as follows:

join EXPR, LIST

Parameter description:

  • EXPR : Connector.

  • LIST :List or array

Example

#/ Usr/bin/perl#
Define string $var_ String="www runoob com"$ Var_ Names="Google, Taobao, runoob, Weibo"#
Convert the string to an array @ string=split ('-', $var_string)@ Names=split (',', $var_names)#
Convert the array to the string $string1=join ('-', @ string)$ String2=join (',', @ names);
Print "$string1 n"; Print "$string2 n";

Execute the above program, and the output is as follows:

www-runoob-com
google,taobao,runoob,weibo

Array sorting

Perl array sorting in the use of sort() function, the syntax formatis as follows:

sort [ SUBROUTINE ] LIST

Parameter description:

  • SUBROUTINE :Specify a rule

  • LIST :List or array

Example

#/ Usr/bin/perl # Define array @ sites=qw (Google Taobao runoob
Facebook; Print "Before sorting: @ sites n"#
Sort the array @ sites=sort (@ sites); Print "After sorting: @ sites n";

Execute the above program, and the output is as follows:

Before sorting: google taobao runoob facebook
After sorting: facebook google runoob taobao

Note: array sorting is based on the ASCII sort by numeric values. So when we sort the array, it’s best to convert each element to lowercase before sorting.

Special variable: $[

Special variable $[ represents the first index value of the array, which is usually 0, if we set the $[ set to 1, the first index value ofthe array is 1, the second is 2, and so on. Examples are as follows:

Example

#/ Usr/bin/perl # Define array @ sites=qw (Google Taobao runoob
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, and the output is as follows:

website: google taobao runoob facebook
@sites[1]: google
@sites[2]: taobao

In general, we do not recommend using special variables. $[ in the new version of Perl, this variable is obsolete.

Merge array

The elements of the array are divided by commas, or we can use commas to merge the arrays, as follows:

Example

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

Execute the above program, and the output is as follows:

numbers = 1 3 4 5 6

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

Example

#!/usr/bin/perl@odd=(1,3,5);@even=(2,4,6);@numbers=(@odd,@even);print"numbers
= @numbers\\n";

Execute the above program, and the output is as follows:

numbers = 1 3 5 2 4 6

Select an element from the list

A list can be used as an array, and specifying an index value after the list reads the specified element, as follows:

Example

#!/usr/bin/perl$var=(5,4,3,2,1)[4];print"var's value =$var\\n"

Execute the above program, and the output is as follows:

var's value = 1

We can also use it in an array .. to read the specified range of elements:

Example

#!/usr/bin/perl@list=(5,4,3,2,1)[1..3];print"list's value = @list\\n";

Execute the above program, and the output is as follows:

list's value = 4 3 2

Powered by TorCMS (https://github.com/bukun/TorCMS).