Perl array _5

Perl scalar value storage array is a list of variables, which can be of different types.

An array of variables that begin with @. Access array elements using the variable name $ + + [index] to read format, the following examples:

#!/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";

Creating an array

Array variables @ sign, the elements in brackets, you can begin to define qw array.

@array = (1, 2, 'Hello');
@array = qw/这是 一个 数组/;

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

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

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

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

Access array elements

[1] to access array elements using the variable name $ + + [index] is read format;
[2] array index starts from 0, i.e., 0 for the first element, a second element, and so on. From the reverse start reading negative, a -1 for the first element, second element -2;
[3] the read element is not present will be undef;

Array serial number

Perl provided by the array may be in the form of a sequence of output format start value + ... + end value, the following examples:

#!/usr/bin/perl
 
@var_10 = (1..10);
@var_20 = (10..20);
@var_abc = (a..z);
 
print "@var_10\n";   # 输出 110
print "@var_20\n";   # 输出 1020
print "@var_abc\n";  # 输出 a 到 z

Array size

[1] size of the array is determined by the context of the scalar array. :

@array = (1,2,3);
print "数组大小: ",标量 @array,"\n";

Returns the array length is the physical size of the array, rather than the number of elements , we can see the following examples:

#!/uer/bin/perl
 
@array = (1,2,3);
$array[50] = 4;
 
$size = @array;
$max_index = $#array;
 
print "数组大小:  $size\n";
print "最大索引: $max_index\n";
执行以上程序,输出结果为:

数组大小:  51
最大索引: 50

From the results of the output, the array element has only four, but the size of the array 51.
[2] The array into a scalar environment can get its length. Some people also prefer the direct use scalar

my $stooge_count = scalar @stooges; # 4

my $stooge_count = @stooges;        # 4

[3] Do not use the length to get the length of the array. You will only get the length of the string.

my $moe_length = length $stooges[@stooges/2];
                 length $stooges[2];
                 length 'Moe';
                 3;

[4] array has no boundaries
array has no finite size, and without prior declaration. Changing the size of the array as needed. Nor sparse array. The following code creates an array of 10,000 elements.

my @array     = ();
$array[10000] = 'x';

@array now has 10,001 elements (0-10,000). It only fill in one, the other 10,000 are undef.

Array will be flat, but not nested

It will be launched as a large flat list an array merge.

my @sandwich       = ( 'PB', 'J' );
my @other_sandwich = ( 'B', 'L', 'T' );
my @ingredients    = ( @other_sandwich, @sandwich );
# ( 'B', 'L', 'T', 'PB', 'J' )

This means that you can not contain an array to another array or hash. In that case, you will need to use a reference.

The list can be expanded with a comma (,)

One of the best features of Perl is at the end of the list can be expanded with a comma. E.g:

my @array = (
    'This thing',
    'That thing',
);

When you edit the code, which makes it easy to add or remove items, because you will not need to deal with the last item as a special case.

my @array = (
    'This thing',
    'That thing',
    'The other thing',
);

Adding and removing array elements

The following table lists common operation function array:

push @ARRAY, LIST The list of values ​​into the end of an array
pop @ARRAY Delete the last value of the array
shift @ARRAY Array pop the first value and returns it. The array index value is also a successively reduced
unshift @ARRAY, LIST The number of elements in the list in front of an array and returns the new array
#!/usr/bin/perl
 
# 创建一个简单数组
@sites = ("google","runoob","taobao");
$new_size = @sites ;#标量环境中返回数组的大小
print "1. \@sites  = @sites\n"."原数组长度 :$new_size\n";
# 在数组结尾添加一个元素
$new_size = push(@sites, "baidu");
print "2. \@sites  = @sites\n"."新数组长度 :$new_size\n";
 
# 在数组开头添加一个元素
$new_size = unshift(@sites, "weibo");
print "3. \@sites  = @sites\n"."新数组长度 :$new_size\n";
 
# 删除数组末尾的元素
$new_byte = pop(@sites);
print "4. \@sites  = @sites\n"."弹出元素为 :$new_byte\n";
 
# 移除数组开头的元素
$new_byte = shift(@sites);
print "5. \@sites  = @sites\n"."弹出元素为 :$new_byte\n";

Here Insert Picture Description

Cutting array

We can cut an array and returns a new array of cut:

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

Requires that a valid array index value index may be positive after the negative, each index value with commas. .. If the index is continuous, it can be used to represent a specified range:

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

Replace array elements

Perl using array element replacement splice () function, the following syntax:

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

@ARRAY:要替换的数组。
OFFSET:起始位置。
LENGTH:替换的元素个数。
LIST:替换元素列表。

The following examples began to replace the array of five elements from the six elements:

#!/usr/bin/perl
 
@nums = (1..20);
print "替换前 - @nums\n";
 
splice(@nums, 5, 5, 21..25); 
print "替换后 - @nums\n";
执行以上程序,输出结果为:

替换前 - 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

String into an array

Perl will convert a string array using the split () function, the following syntax:

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

参数说明:
PATTERN:分隔符,默认为空格。
EXPR:指定字符串数。
LIMIT:如果指定该参数,则返回该数组的元素个数。
#!/usr/bin/perl
 
# 定义字符串
$var_test = "runoob";
$var_string = "www-runoob-com";
$var_names = "google,taobao,runoob,weibo";
 
# 字符串转为数组
@test = split('', $var_test);#分割符为空格,字符串 "runoob"将转换为宽度为6的数组
@string = split('-', $var_string);#分割符为“-”字符串"www-runoob-com"转换为宽度为3的数组
@names  = split(',', $var_names);#分割符为,字符串"google,taobao,runoob,weibo"转换为宽度为4的数组
 
print "$test[3]\n";  # 输出 o
print "$string[2]\n";  # 输出 com
print "$names[3]\n";   # 输出 weibo
执行以上程序,输出结果为:

o
com
weibo

Converting a string array

Perl in the array to a string using the join () function, the following syntax:

join EXPR, LIST

参数说明:
EXPR:连接符。
LIST:列表或数组。
#!/usr/bin/perl
 
# 定义字符串
$var_string = "www-runoob-com";
$var_names = "google,taobao,runoob,weibo";
 
# 字符串转为数组
@string = split('-', $var_string);
@names  = split(',', $var_names);
 
# 数组转为字符串
$string1 = join( '-', @string );
$string2 = join( ',', @names );
 
print "$string1\n";
print "$string2\n";
执行以上程序,输出结果为:

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

Sorting an array

Perl using the sorted array sort () function, the following syntax:

sort [ SUBROUTINE ] LIST

参数说明:
SUBROUTINE:指定规则。
LIST:列表或数组。
#!/usr/bin/perl
 
# 定义数组
@sites = qw(google taobao runoob facebook);
print "排序前: @sites\n";
 
# 对数组进行排序
@sites = sort(@sites);
print "排序后: @sites\n";
执行以上程序,输出结果为:

排序前: google taobao runoob facebook
排序后: facebook google runoob taobao

Note: Sorting an array is sorted according to the ASCII numeric value. So we had better first converted to lowercase and then each element in the sort to sort the array.

Array merge

Element of the array is a comma to separate, we can also use commas to merge array, as follows:

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

执行以上程序,输出结果为:

numbers = 1 3 4 5 6

A plurality of arrays can also be embedded in an array, and merged into the master array:

#!/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

Select an element from the list

A list can be used as an array, index is specified in the list of the specified element can be read as follows:

#!/usr/bin/perl
 
$var = (5,4,3,2,1)[4];#这里的4为索引值
 
print "var 的值为 = $var\n"
执行以上程序,输出结果为:

var 的值为 = 1

Similarly, we can use in an array ... read element specifies the range of:

#!/usr/bin/perl
 
@list = (5,4,3,2,1)[1..3];
 
print "list 的值 = @list\n";
执行以上程序,输出结果为:

list 的值 = 4 3 2

References:
[1] https://www.runoob.com/perl/perl-arrays.html

Published 71 original articles · won praise 8 · views 7299

Guess you like

Origin blog.csdn.net/qq_43042339/article/details/104609807