haimei has asked for the wisdom of the Perl Monks concerning the following question:

My program requirs to read an input file with 3 columns(month dates, fileSize, fileName)in 3 steps: Here I only sample 2 months dates in Julian year order to give an example.

  1. count the total number of files loaded in each month.
  2. count the total file size for each month.
  3. count the largest file size in each month.

I do need help to correct my code; when I run it, it doesn't go to output file, and the output is not in individual month but in whole months. Much appreciated if anyone could improve my code to approach the solution.

Here is my input file format.

001 175 FILENAME
002 1856 FILENAME
003 177 FILENAME
032 175 FILENAME
033 2345 FILENAME
034 175 FILENAME

Here is my code:

#!/usr/bin/perl use strict; use warning; use Data::Dumper; use File::Find; use File::stat; use sort 'stable'; my $filin = '/root/scripts/newsort.in'; my $fleot = '/root/scripts/results/size.out'; open my $fh, $filin || die $!; open my $fot, ">$fleot" || die $!; ##Define month lengths @Janlen = ( '006', '007', '008', '009', '010', '011', '012', '013', '0 +14', '015', '016', '017', '018', '019', '020', '021', '022', '023', ' +024', '025', '026', '027', '028', '029', '030', '031' ); @Feblen = ( '032', '033', '034', '035', '036', '037', '038', '039', '0 +40', '041', '042', '043', '044', '045', '046', '047', '048', '049', ' +050', '051', '052', '053', '054', '055', '056', '057', '058', '059' ) +; #Define month hash %mthlens = (@Janlen, @Feblen); my @julens = %mthlens; my $julias = @julens; my $Janlias = @Janlen; my $Feblias = @Feblen; my $Marlias = @Marlen; while (%mthlens=<$fh>){ chomp; my %lengths = map { $_ => length $_ } %mthlens; while ( my ($Janlen,$length,$filename) = each %lengths) { @s = sort { $length{$b} <=> $length{$a}} keys %length; print join("\t", $Janlen, $length, $filename ), "\n"; } }

Replies are listed 'Best First'.
Re: Help! Stuck on methods to count file size.
by PBeginner (Initiate) on Oct 10, 2013 at 05:29 UTC

    I guess your print statement must be as

    print $fot join("\t",$Janlen, $length, $filename), "\n";

    Hope this will do the needful.

      Great!!! I got the result in the output file, thank you so much!. But I need the output in Desending order by file size for individual month. The month date and file name should automatically match their file size as well, so I can find the largest file with its date and file name. I am not sure which code can handle it.

      Here is the output:

      File sizes are displayed in the second column. I am not sure what are the numbers following FILENAME, such as 38, 33 , 38 ....

      Thank you for your wisdom, please enlighten me again!

      024 710 FILENAME 38 114 923 FILENAME 33 044 367 FILENAME 38 083 7864 FILENAME 39 153 783 FILENAME 33 084 864 FILENAME
        Search, read about sort rather than ask the Monks to do your work.
      Sorry, I need the solution as soon as possible, so my friend registered the other site for me too. I apologize I made the this confusion. Please keeep helping me... Thanks a lot!