打卡9:perl grepmap

打卡9:perl grepmap,第1张

概述1 while (<>) { @d=grep {/$_/} @b;-----------------注意此处的$_已经是grep中的$_了,不是while的。 $l=@d; if ($l==0) { push (@b,$_); }}改为 while(my $line=<>){ @d = grep{ 1
while (<>) {        @d=grep {/$_/} @b;-----------------注意此处的$_已经是grep中的$_了,不是while的。        $l=@d;        if ($l==0) {                push (@b,$_);        }}
改为
while(my $line=<>){  @d = grep{ $line ne $_} @b;  push @d,$line;}
2
my @b=();my @d=();#my %a;while (<>) {        @d=grep {/$_/} @b;        $l=@d;        print "1:l=$l:d=@d:b=@b;cu=$_\n";        if ($l==0) {                push (@b,$_);        }}

3

map
print map {"$_\n";} 0..9; 
表示计算.
不是输入而是将结果放到数组中.
然后用print将数组中的内容print出来

===================================

find 递归

#!/opt/exp/bin/perl -wuse strict;use warnings;use file::Find;my ($size,$dircnt,$filecnt) = (0,0);sub process {my $file = $file::Find::name;if (-d $file) {$dircnt++;}else {$filecnt++;$size += -s $file;}print "line17:$file\n";}find(\&process,'.');print "$filecnt files,$dircnt directory. $size bytes. ";


自己的理解:

 参数2是init的path,find会自己foreach 文件,然后call子函数;然后发现目录,在自动递归。
同比shell的find是 find * -name "*" -print. 也在自动递归

find实现,不用find函数,递归

#!/opt/exp/bin/perl -wuse strict;use warnings;sub lsr($) {    sub lsr;    my $cwd = shift;    local *DH;    if (!opendir(DH,$cwd)) {        warn "Cannot opendir $cwd: $! $^E";        return undef;    }    foreach (readdir(DH)) {        if ($_ eq '.' || $_ eq '..') {            next;        }        my $file = $cwd.'/'.$_;        if (!-l $file && -d _) {            $file .= '/';            lsr($file);        }       # process($file,$cwd);        process($file);    }    closedir(DH);}my ($size,0);sub process($$) {    my $file = shift;    print $file,"\n";    if (substr($file,length($file)-1,1) eq '/') {        $dircnt++;    }    else {        $filecnt++;        $size += -s $file;    }}lsr('.');print "$filecnt files,$dircnt directory. $size bytes.\n";

find实现,不用find函数,非递归

#!/opt/exp/bin/perl -wuse strict;use warnings;sub lsr_s($) {    my $cwd = shift;    my @dirs = ($cwd.'/');    my ($dir,$file);    while ($dir = pop(@dirs)) {        local *DH;        if (!opendir(DH,$dir)) {            warn "Cannot opendir $dir: $! $^E";            next;        }        foreach (readdir(DH)) {            if ($_ eq '.' || $_ eq '..') {                next;            }            $file = $dir.$_;                     if (!-l $file && -d _) {                $file .= '/';                push(@dirs,$file);            }           # process($file,$dir);            process($file);        }        closedir(DH);    }}my ($size,0);sub process($$) {    my $file = shift;    print $file,1) eq '/') {        $dircnt++;    }    else {        $filecnt++;        $size += -s $file;    }}lsr_s('.');print "$filecnt files,$dircnt directory. $size bytes.\n";
总结

以上是内存溢出为你收集整理的打卡9:perl grep/map全部内容,希望文章能够帮你解决打卡9:perl grep/map所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1276785.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存