- 论坛徽章:
- 0
|
本帖最后由 moxnet 于 2011-07-22 18:38 编辑
代码逻辑有问题. 你的数值是从1开始的, 而数组的下标是从0开始的,这儿引起的问题- #!/usr/bin/perl
- use Data::Dumper;
- use strict;
- use warnings;
- my @a = qw/1 2 3 4 5/;
- my %h = map {
- print join(',', $a[$_], $_),"\n";
- $a[$_] => $_ ; } @a;
- print Dumper(\@a);
- print Dumper(\%h);
复制代码 输出:- :! perl t
- 2,1
- 3,2
- 4,3
- 5,4
- Use of uninitialized value in join or string at t line 7.
- ,5
- Use of uninitialized value in list assignment at t line 7.
- $VAR1 = [
- 1,
- 2,
- 3,
- 4,
- 5
- ];
- $VAR1 = {
- '' => 5,
- '4' => 3,
- '3' => 2,
- '2' => 1,
- '5' => 4
- };
复制代码 如果原始数组的元素是从0开始的, 输出就是ok的
:! perl t
0,0
1,1
2,2
3,3
4,4
5,5
$VAR1 = [
0,
1,
2,
3,
4,
5
];
$VAR1 = {
'4' => 4,
'1' => 1,
'3' => 3,
'0' => 0,
'2' => 2,
'5' => 5
}; |
|