Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 打印时,按年度和月份对文件大小求和,并包括年度总计_File_Perl - Fatal编程技术网

File 打印时,按年度和月份对文件大小求和,并包括年度总计

File 打印时,按年度和月份对文件大小求和,并包括年度总计,file,perl,File,Perl,在这个问题上,我遵循@zdim的递归解决方案 以按年度和月份汇总我的文件大小 perl -MFile::Find -E' use POSIX; $dir = shift // "."; find( sub { return if /^\.\.?$/; my ($mtime, $size) = (stat)[9,7]; my $yearmon = strftime "%Y%m",localtime($mtime);

在这个问题上,我遵循@zdim的递归解决方案 以按年度和月份汇总我的文件大小

perl -MFile::Find -E' use POSIX;
    $dir = shift // "."; 
    find( sub { 
        return if /^\.\.?$/; 
        my ($mtime, $size) = (stat)[9,7];
        my $yearmon = strftime "%Y%m",localtime($mtime);
        $rept{$yearmon} += $size ;
    }, $dir ); 
    say ("$_ => $rept{$_} bytes") for sort keys %rept
'
它给出了这样的结果

201701 => 7759 bytes
201702 => 530246 bytes
201703 => 3573094 bytes
201704 => 425827 bytes
201705 => 3637771 bytes
201706 => 2325391018 bytes
201707 => 127005 bytes
201708 => 2303 bytes
201709 => 231465431 bytes
201710 => 273667 bytes
201711 => 6397659 bytes
201712 => 333587 bytes
201802 => 874676 bytes
201803 => 147825681 bytes
201804 => 84971454 bytes
201805 => 3483547 bytes
201806 => 8004797 bytes
201807 => 184676 bytes
201808 => 1967947 bytes
201809 => 1592310 bytes
201810 => 24176 bytes
201811 => 883378 bytes
201812 => 6661592 bytes
201901 => 33979401 bytes
但我需要在打印给定年份中所有可用月份后,包括年度总计,如下所示

201710 => 1111 bytes
201711 => 2222 bytes
201712 => 3333 bytes
2017  => 6666 bytes ( summed up )
201803 => 11111 bytes
201809 => 22222 bytes
2018  => 33333 bytes ( summed up )

我怎么知道的?。一年一个月可能有空格,并且每年不必以第12个月结束。

鉴于您已经在代码的第一部分构建了%rept散列,显示部分应该如下所示:

# Keep track of current year and current year total
my ($year, $year_total);

for (sort keys %rept) {
  # Get the year from the current record
  my $this_year = substr($_, 0, 4);
  # If the year has changed, then print a total line
  # and reset the year total.
  if (defined $year and $year ne $this_year) {
    say "$year   => $year_total bytes";
    $year_total = 0;
  }
  # Add to the year total and store the current year
  $year_total += $rept{$_};
  $year = $this_year;

  say "$_ => $rept{$_} bytes";
}

# Final year total line
say "$year   => $year_total bytes";

鉴于您已经在代码的第一部分构建了%rept哈希,显示部分应该是这样的:

# Keep track of current year and current year total
my ($year, $year_total);

for (sort keys %rept) {
  # Get the year from the current record
  my $this_year = substr($_, 0, 4);
  # If the year has changed, then print a total line
  # and reset the year total.
  if (defined $year and $year ne $this_year) {
    say "$year   => $year_total bytes";
    $year_total = 0;
  }
  # Add to the year total and store the current year
  $year_total += $rept{$_};
  $year = $this_year;

  say "$_ => $rept{$_} bytes";
}

# Final year total line
say "$year   => $year_total bytes";

两个小小的改变就可以了

perl -MFile::Find -E' use POSIX;
    $dir = shift // "."; 
    find( sub { 
        return if /^\.\.?\z/;                             # Fixed $ -> \z
        my ($mtime, $size) = (stat)[9,7];
        my $yearmon = strftime "%Y%m",localtime($mtime);
        $rept{$yearmon} += $size;
        $rept{substr($yearmon, 0, 4)} += $size;           # <---
    }, $dir ); 
    say "$_ => $rept{$_} bytes"
        for sort { "${a}99" cmp "${b}99" } keys %rept;    # <---
'

两个小小的改变就可以了

perl -MFile::Find -E' use POSIX;
    $dir = shift // "."; 
    find( sub { 
        return if /^\.\.?\z/;                             # Fixed $ -> \z
        my ($mtime, $size) = (stat)[9,7];
        my $yearmon = strftime "%Y%m",localtime($mtime);
        $rept{$yearmon} += $size;
        $rept{substr($yearmon, 0, 4)} += $size;           # <---
    }, $dir ); 
    say "$_ => $rept{$_} bytes"
        for sort { "${a}99" cmp "${b}99" } keys %rept;    # <---
'

有什么问题吗?考虑到您已经拥有的代码,这看起来是您应该能够做到的。一种方法是使用散列,其中年是第一个键,月是第二个键,因此您将编写类似$rept{$year}->{$month}+=$size的内容。这有点冗长,但绝对合理。@Dada。。是的,你说得对。。但是现在我意识到我会错过池上春树的破解…有什么问题吗?考虑到您已经拥有的代码,这看起来是您应该能够做到的。一种方法是使用散列,其中年是第一个键,月是第二个键,因此您将编写类似$rept{$year}->{$month}+=$size的内容。这有点冗长,但绝对合理。@Dada。。是的,你说得对。。但是现在我意识到我会错过池上春树的攻略。@stack0114106:这可以通过两个小改动来解决。请看我的答案。@stack0114106:这可以通过两个小改动来解决。看看我的答案。