在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1 数组与引用此处引用相当于C中的指针。 2 声明的区别
my @AoA = ( [ "fred", "barney", "pebbles", "bambam", "dino", ], [ "george", "jane", "elroy", "judy", ], [ "homer", "bart", "marge", "maggie", ], );
$ref_to_AoA = [ [ "fred", "barney", "pebbles", "bambam", "dino", ], [ "george", "jane", "elroy", "judy", ], [ "homer", "bart", "marge", "maggie", ], ]; 3 访问的区别
$AoA[$i][$j] 因为第一列数组里面存放的是引用,所以还可以这样访问:
$AoA[$i]->[$j]
$ref_AoA->[$i][$j] 同理引用还可以这样访问:
$ref_AoA->[$i]->[$j] 4 添加行元素
my (@AoA, $ref_to_AoA); sub print_AoA{ for (@AoA) { print "@{$_}\n"; } print "\n"; } # assign to our array, an array of array references @AoA = ( [ "fred", "barney", "pebbles", "bambam", "dino", ], [ "george", "jane", "elroy", "judy", ], [ "homer", "bart", "marge", "maggie", ], ); say $AoA[2][1]; $ref_to_AoA = [ [ "fred", "barney", "pebbles", "bambam", "dino", ], [ "george", "jane", "elroy", "judy", ], [ "homer", "bart", "marge", "maggie", ], ]; print_AoA(); my @tmp = (1, 2, 3, 4); push @AoA, [@tmp]; # 因为数组AoA第一列需要的是引用,而@tmp是数组,直接赋值会出错。[]表示返回@tmp的引用,即把@tmp的引用push到@AoA最后一行,二维数组行数加1. print_AoA(); push @AoA, @tmp; print_AoA(); 覆盖行
#$AoA[0] = @tmp; #$AoA[0]是scalar型,而@tmp是list型,所以用默认把tmp的个数赋给$AoA[0],即$AoA[0]=4; $AoA[0] = [@tmp]; #overwirte print_AoA(); 5 添加列元素
push @{$AoA[0]}, "wilma", "betty"; 省略@{}
use v5.14; # needed for implicit deref of array refs by array ops push $AoA[0], "wilma", "betty"; # 在5.14版本之前不能通过,因为以前规定push的第一参数必须为数组。在新版本中当$AoA[0]中存在引用时可以通过,但无引用时不正确。 print_AoA(); my $aref = undef; #push $aref, qw/some value/; # 出错:Not an ARRAY reference my $aref = [@tmp]; push $aref, qw/some value/; # 正确,因为aref此时不是个空引用 print "$aref : @$aref\n"; 6 访问与打印6.1 运算符优先级
$@*%& 6.2 访问一个
print $AoA[$i][$j]; print ref_$AoA->[$i]->[$j]; 6.3 遍历
for $aref ( @AoA ) # $aref只是第一列里面的引用,要想访问整行必须加@,又$访问级别比@高,所以()可以省略。 { say "\t [ @$aref ],"; }
for my $i (0 .. $#AoA){ say "elt $i is @{$AoA[$i]}"; }
for my $i (0 .. $#AoA){ for my $j (0 .. $#{$AoA[$i]}){ say "elt $i, $j is $AoA[$i][$j]\n"; } } 7 切片要访问几行几列元素。和Matlab中访问矩阵的方法差不多。
my @part = (); my $x = 4; for (my $y = 1; $y<4; $y++){ push @part, $AoA[$x][$y]; } # 简单写法 @part = @{$AoA[4]}[1..4];
my @newAoA = (); for (my $startx= my $i = 1; $i<=5; $i++){ for(my $starty = my $j = 2; $j<=4; $j++){ $newAoA[$i - $startx][$j - $starty] = $AoA[$i][$j]; } } #一个循环简单写法 for (my $x = 1; $x<=5; $x++){ push @newAoA, [@{$AoA[i]}[2 .. 4]]; }
sub splice_2D{ my $lrr = shift; my($x_l, $x_h, $y_l, $y_h) = @_; return map( [ @{$lrr -> [$_]} {$y_l .. $y_h}] )$x_l .. $x_h; } @newAoA = splice_2D(\@AoA, 1=>5, 2=>4); Date: 2011-10-29 15:00:34 HTML generated by org-mode 6.33x in emacs 23 |
请发表评论