perl 引用/hash

来源:互联网 发布:正品良心淘宝日本代购 编辑:程序博客网 时间:2024/05/19 18:43

#!/opt/exp/bin/perl -w#array init@a=();@LoL=();sub function {        $j=@_;        @a=("1","2","3");        print "j=$j\n";        return @a; }for $i (1..10) {        @list = function ($i);        #below two is the same ,it is array reference        $LoL[$i] = \@list;# it is the same with above code#       $LoL[$i] = [ @list ];# it is array number       $LoL[$i] = scalar @list;        #notes add {} ,reback to array        print "2:@{$LoL[$i]} \n";        #print "2:$LoL[$i] \n";}

2:

#!/opt/exp/bin/perl -wsub printSorted {·#parameter1 is reference for array, parameter2 is hash.@_ list assign        my ($data,%options) = @_;        # get the value for array         print "$data->[3]\n";#get hash value        print "$options{type}\n";}#$list={"1","2","3","4"};@list=("1","2","3","4");printSorted (\@list,dir => 'asc',type => 'numerical');

3

#!/opt/exp/bin/perl -wmy @array1 = (10, 20, 30, 40, 50); my @array2 = ( 1, 2, \@array1, 3, 4);%hash=(abc=>123, def=>456); #reference to hash$ref=\%hash; #point to hashprint "%$ref\n"; print "$$ref{abc}\n";
4

<span style="font-size:14px;">#!/opt/exp/bin/perl -w#it doesn't use $_ in hash and array#array#set value@list = (4,3,2,1,0);foreach $item (@list) {        printf ("%d->$item\n",$i++)}#hashwhile (my ($key,$value) = each (%ENV)) {        print "$key - $value\n";}my @val =sort keys %ENV;for ($i=0;$i<@val;$i++) {        $key=$val[$i];        print "$key-----$ENV{$key} \n";}</span>

==
在将hash的keys  sort 后,sort是一个独立的循环操作.先这个函数。
map是一个独立的循环,输出key和value,
即按比较顺序输出key-value

print map {"$_ -> $hash{$_}"} sort {$a cmp $b} keys %hash; 
应该是print if aa ; 这种单句处理
上面的sort是一个执行


0 0
原创粉丝点击