Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Perl Arrays

Perl Arrays

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

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

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

"

;

In the program, the $ symbol is escaped with \ so it is output as-is.

Running the above program, the output is:



$hits[0] = 25

$hits[1] = 30

$hits[2] = 40

$names[0] = google

$names[1] = runoob

$names[2] = taobao


Creating an Array

Array variables begin with @ symbol, with elements placed in parentheses; you can also define an array starting with qw .



@array = (1, 2, 'Hello');

@array = qw/这是 一个 数组/;

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



@days = qw/google

taobao

...

runoob/;

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



$array[0] = 'Monday';

...

$array[6] = 'Sunday';


Accessing Array Elements

Accessing array elements uses $ + variable name + [index value] format to read, as shown in the following example:

Example

@sites

=

qw/

google taobao runoob

/

;

print

"

$sites

[0]

\n

"

;

print

"

$sites

[1]

\n

"

;

print

"

$sites

[2]

\n

"

;

print

"

$sites

[-1]

\n

"

;

negative numbers to read in reverse

Running the above program, the output is:



google

taobao

runoob

runoob

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

Negative numbers read from the reverse direction, where -1 is the first element and -2 is the second element


Array sequences

Perl provides an array form that can be output as a sequence, in the format start value + .. + end value; the example is as follows:

Example

#!/usr/bin/perl

@var_1

0

=

(

1

.

.10

)

;

@var_2

0

=

(

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

Running 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 size of an array is determined by the scalar context of the array:

Example

@array

=

(

1

,

2

,

3

)

;

print

"

Array size:

"

,

scalar

@array

,

"

\n

"

;

Running the above program, the output is:

数组大小: 3

The array length returns the physical size of the array, not the number of elements, as we can see in 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

"

;

Running the above program, the output is:

数组大小:  51

最大索引: 50

From the output, we can see that the array has only four elements, but the array size is 51.


Adding and removing array elements

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

If you have no prior programming experience, you might ask what a function is. In fact, the print we used earlier is an output function.

The following table lists commonly used functions for operating on arrays:

No. Type and description
1 push @ARRAY, LIST

Put the values of a list at the end of the array

2 pop @ARRAY

Remove the last value of the array

3 shift @ARRAY

Pop the first value of the array and return it. The array index values also decrease by one accordingly.

4 unshift @ARRAY, LIST

Put a list at the front of the array and return the number of elements in the new array.

Example

#!/usr/bin/perl

Create a simple array

@sites

=

(

"

google

"

,

"

runoob

"

,

"

taobao

"

)

;

$new_size

=

@sites

;

print

"

@

sites = @sites

\n

"

.

"

Original array length:

$new_size

\n

"

;

Add an element at the end of the array

$new_size

=

push

(

@sites

,

"

baidu

"

)

;

print

"

@

sites = @sites

\n

"

.

"

New array length:

$new_size

\n

"

;

Add an element at the beginning of the array

$new_size

=

unshift

(

@sites

,

"

weibo

"

)

;

print

"

@

sites = @sites

\n

"

.

"

New array length:

$new_size

\n

"

;

Remove the element at the end of the array

$new_byte

=

pop

(

@sites

)

;

print

"

@

sites = @sites

\n

"

.

"

Popped element:

$new_byte

\n

"

;

Remove the element at the beginning of the array

$new_byte

=

shift

(

@sites

)

;

print

"

@

sites = @sites

\n

"

.

"

Popped element:

$new_byte

\n

"

;

Running the above program, the output is:


Slicing arrays

We can slice an array and return the new sliced array:

Example

#!/usr/bin/perl

@sites

=

qw/

google taobao runoob weibo qq facebook NetEase

/

;

@sites2

=

@sites

[

3

,

4

,

5

]

;

print

"

@sites2

\n

"

;

Running the above program, the output is:



weibo qq facebook

Array slicing requires specifying valid index values, which can be positive or negative, with each index value separated by a comma.

If the indexes are consecutive, you can use .. 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

"

;

Running the above program, the output is:



weibo qq facebook


Replacing array elements

In Perl, array element replacement uses the splice() function, with the following syntax:



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

Parameter Description:

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

Example

#!/usr/bin/perl

@nums

=

(

1

.

.20

)

;

print

"

Before replacement - @nums

\n

"

;

splice

(

@nums

,

5

,

5

,

21

.

.25

)

;

print

"

After replacement - @nums

\n

"

;

Running the above program, the output is:



替换前 - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

替换后 - 1 2 3 4 5 21 22 23 24 25 11 12 13 14 15 16 17 18 19 20


Converting a string to an array

In Perl, converting a string to an array uses the split() function, with the following syntax:



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

Parameter Description:

Example

#!/usr/bin/perl

Define a string

$var_test

=

"

runoob

"

;

$var_string

=

"

www-runoob-com

"

;

$var_names

=

"

google,taobao,runoob,weibo

"

;

Converting a string to an 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

"

;

Output weibo

Running the above program, the output is:



o

com

weibo


Converts the array to a string

In Perl, converting an array to a string uses the join() function, with the following syntax:



join EXPR, LIST

Parameter Description:

Example

#!/usr/bin/perl

Define a string

$var_string

=

"

www-runoob-com

"

;

$var_names

=

"

google,taobao,runoob,weibo

"

;

Converting a string to an array

@string

=

split

(

'

'

,

$var_string

)

;

@names

=

split

(

'

,

'

,

$var_names

)

;

Array to string

$string1

=

join

(

'

'

,

@string

)

;

$string2

=

join

(

'

,

'

,

@names

)

;

print

"

$string1

\n

"

;

print

"

$string2

\n

"

;

Running the above program, the output is:



www-runoob-com

google,taobao,runoob,weibo


Array sorting

In Perl, array sorting uses the sort() function, with the following syntax:



sort [ SUBROUTINE ] LIST

Parameter Description:

Example

#!/usr/bin/perl

Defining an array

@sites

=

qw(

google taobao runoob facebook

)

;

print

"

Before sorting: @sites

\n

"

;

Sort the array

@sites

=

sort

(

@sites

)

;

print

"

After sorting: @sites

\n

"

;

Running the above program, the output is:



排序前: google taobao runoob facebook

排序后: facebook google runoob taobao

Note: Array sorting is based on ASCII numeric values. Therefore, when sorting an array, it's best to first 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 $[ to 1, then the first index value of the array is 1, the second is 2, and so on. Example below:

Example

#!/usr/bin/perl

Defining an array

@sites

=

qw(

google taobao runoob facebook

)

;

print

"

Websites: @sites

\n

"

;

Set the first index of the array to 1

$

[

=

1

;

print

"

@

sites[1]:

$sites

[1]

\n

"

;

print

"

@

sites[2]:

$sites

[2]

\n

"

;

Running the above program, the output is:



网站: google taobao runoob facebook

@sites[1]: google

@sites[2]: taobao

In general, we do not recommend using the special variable $[, which has been deprecated in newer versions of Perl.


Concatenate arrays

Array elements are separated by commas, and we can also use commas to merge arrays, as shown below:

Example

#!/usr/bin/perl

@numbers

=

(

1

,

3

,

(

4

,

5

,

6

)

)

;

print

"

numbers = @numbers

\n

"

;

Running the above program, the output is:



numbers = 1 3 4 5 6

You can also embed multiple arrays within 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

"

;

Running the above program, the output is:



numbers = 1 3 5 2 4 6


Selecting elements from a list

A list can be used as an array; by specifying an index value after the list, you can read the specified element, as shown below:

Example

#!/usr/bin/perl

$var

=

(

5

,

4

,

3

,

2

,

1

)

[

4

]

;

print

"

The value of var is =

$var

\n

"

Running the above program, the output is:



var 的值为 = 1

Similarly, we can use .. in an array to read elements in a specified range:

Example

#!/usr/bin/perl

@list

=

(

5

,

4

,

3

,

2

,

1

)

[

1

.

.3

]

;

print

"

The value of list = @list

\n

"

;

Running the above program, the output is:



list 的值 = 4 3 2