my @list = qw /1 2 3 2 1 4 aa a bb c b bb d/;foreach (@list) { print"$_ "; }# 1 2 3 2 1 4 aa a bb c b bb dmy %hash;my @uniq = grep { ++$hash{$_} < 2 } @list;foreach (@uniq) { print"$_ "; }# 1 2 3 4 aa a bb c b d
use List::MoreUtils qw(uniq);#use List::MoreUtils ':all';my @list = qw /1 2 3 2 1 4 aa a bb c b bb d/;foreach (@list) { print"$_ "; }# 1 2 3 2 1 4 aa a bb c b bb dmy @uniq = uniq(@list);foreach (@uniq) { print"$_ "; }# 1 2 3 4 aa a bb c b d
use strict;subgetparameter{my $i;for($i=0;$i<=$#_;$i++) {print"It's the ";print $i+1;print"parameter: $_[$i] \n"; }}my @array=("this","is","a","test");my $variable="this is another test";&getparameter($variable,@array);
use strict;subgetparameter{ (my @arr,my $var)=@_;print"It's the 1st parameter: @arr \n";print"It's the 2nd parameter: $var \n";}my @array=("this","is","a","test");my $variable="this is another test";&getparameter(@array,$variable);
use strict;subgetparameter{ (my $var,my @arr)=@_;print"It's the 1st parameter: $var \n";print"It's the 2nd parameter: @arr \n";}my @array=("this","is","a","test");my $variable="this is another test";&getparameter($variable,@array);
如果要传递2个数组怎么办???可以采用引用的方式:
use strict;subgetparameter{ (my $arr1,my $arr2)=@_;print"It's the 1st parameter: @$arr1 \n";print"It's the 2nd parameter: @$arr2 \n";}my @array1=("this","is","a","test");my @array2=qw/this is another test/;&getparameter(\@array1,\@array2);
An arrow (->) followed by a square ([]) or curly brackets ({}) can be used to directly access the elements of an array or a hash referenced by a certain hash. For instance: $array_ref->[5] will retrieve the 5th element of the array pointed to by $array_ref.
An example:
do"lol.pl"; # Load the other file (foo)my $cont = get_contents();print $cont->{'title'}, "\n";print $cont->{'subs'}->[0]->{'url'}, "\n";print $cont->{'subs'}->[0]->{'subs'}->[1]->{'title'}, "\n";
Note that the arrows following the first arrow are optional as perl sees that the programmer wishes to access the subseqeunt sub-items. However, the first one is mandatory because the expression $array_ref{'elem'} looks for the hash %array_ref.
$z = "time hcat to feed the cat hcat";$z =~ s/cat/AAA/g;print $z,"\n"; # time hAAA to feed the AAA hAAA$z =~ s/cat|the/AAA/g;print $z,"\n"; # time hAAA to feed AAA AAA hAAA
s///e
## reverse all the words in a string$x = "the cat in the hat";$x =~ s/(\w+)/reverse $1/ge; # $x contains "eht tac ni eht tah"## convert percentage to decimal$x = "A 39% hit rate";$x =~ s!(\d+)%!$1/100!e; # $x contains "A 0.39 hit rate"## s/// 可以用 s!!! , s{}{} , s{}// 进行替换