in reply to Help! Stuck on methods to count file size.

well that's like super ugly
but working:
%jan = ('max' => 0, 'bytes' => 0, 'files' => 0); %feb = ('max' => 0, 'bytes' => 0, 'files' => 0); for (6..31) {$m[$_] = \%jan} for (32..59) {$m[$_] = \%feb} while(<DATA>){ ($day, $value, $name) = split / /; print "$day $value\n"; ${$m[$day]}{'max'} < $value ? (${$m[$day]}{'max'} = $value, ${$m[$day]}{'bytes'} += $value, ++${$m[$day]}{'files'}):( ${$m[$day]}{'bytes'} += $value, ++${$m[$day]}{'files'}) ; } print "jan\n"; foreach (sort keys %jan){ print "$_ $jan{$_}\n"; } print "feb\n"; foreach (sort keys %feb){ print "$_ $feb{$_}\n"; } __DATA__ 006 175 FILENAME 007 1856 FILENAME 008 177 FILENAME 032 175 FILENAME 033 2345 FILENAME 034 175 FILENAME

Replies are listed 'Best First'.
Re^2: Help! Stuck on methods to count file size.
by Anonymous Monk on Oct 11, 2013 at 02:38 UTC
    Dear Lennotoecom, thank you very much for your beautiful code, it's really helpful!!!
Re^2: Help! Stuck on methods to count file size.
by haimei (Novice) on Oct 11, 2013 at 04:40 UTC
    Great Monk, your code is Key->Important->Simple->Powerful! Thanks again!
Re^2: Help! Stuck on methods to count file size.
by haimei (Novice) on Dec 04, 2013 at 17:18 UTC

    Dear Master, based on your previous code, I need to count individual type of files beside the whole files from system via the txt file. I tried in this way as below but it doesn't show the correct number of individual type of files, please point out the cause of mistake or provide me the correction, thanks!

    my $goodfile = ' good.txt ' my $badfile = 'bad.txt' %jan = ('max' => 0, 'bytes' => 0, 'files' => 0, 'gfiles =>0, 'bfiles = +> 0); %feb = ('max' => 0, 'bytes' => 0, 'files' => 0, 'gfiles => 0, 'bfiles +=> 0); for (6..31) {$m[$_] = \%jan} for (32..59) {$m[$_] = \%feb} while(<DATA>){ if($goodfile='gfiles') {($day, $value, $name) = split / /; print "$day $value\n"; ${$m[$day]}{'max'} < $value ? ${$m[$day]}{'max'} = $value, ${$m[$day]}{'bytes'} += $value, ++${$m[$day]}{'gfiles'}):( ${$m[$day]}{'bytes'} += $value, ++${$m[$day]}{'gfiles'}) ; } print "jan\n"; foreach (sort keys %jan){ print "$_ $jan{$_}\n"; } print "feb\n"; foreach (sort keys %feb){ print "$_ $feb{$_}\n"; } __DATA__ 006 175 FILENAME 006 176 good.txt 006 12 bad.txt 007 1856 FILENAME 007 1854 good.txt 008 172 bad.txt 008 177 FILENAME 008 23 good.txt 010 42 bad.txt 032 175 FILENAME 033 2345 FILENAME 032 318 good.txt 033 100 bad.txt 034 175 FILENAME
    download
      here's the code
      I also changed my previous mistakes with dereferencing
      feed back if there are any mistakes
      much appreciated
      %jan = ('max' => 0, 'bytes' => 0); %feb = ('max' => 0, 'bytes' => 0); for (6..31) {$m[$_] = \%jan} for (32..59) {$m[$_] = \%feb} while(<DATA>){ ($day, $value, $name) = split /\s+|$/; print "$day $value $name\n"; $m[$day]->{'max'} < $value ? ($m[$day]->{'max'} = $value, $m[$day]->{'bytes'} += $value, ++$m[$day]->{'files'}):( $m[$day]->{'bytes'} += $value, ++$m[$day]->{'files'}) ; $m[$day]->{$name}++; } print "jan\n"; foreach (sort keys %jan){ print "$_ $jan{$_}\n"; } print "feb\n"; foreach (sort keys %feb){ print "$_ $feb{$_}\n"; } __DATA__ 006 175 FILENAME 006 176 good.txt 006 12 bad.txt 007 1856 FILENAME 007 1854 good.txt 008 172 bad.txt 008 177 FILENAME 008 23 good.txt 010 42 bad.txt 032 175 FILENAME 033 2345 FILENAME 032 318 good.txt 033 100 bad.txt 034 175 FILENAME

        Dear Master, it's a creative posting again, I feel very much appreciated your guidance. Your code is critical and powerful!

Re^2: Help! Stuck on methods to count file size.
by Anonymous Monk on Oct 11, 2013 at 02:16 UTC
    It's great post! It works nicely! But I need the file name following the max size too if possible... So much appreciated:-)
      I need pancakes
      well, then you should add another element "name" into the hashes
      and in the while-cycle if current max is found rewrite it,
      exactly the same as the 'max' itself
        It works very well, very much appreciated you keeping helping me!!!
        I should let you know this morning I got the result as following:
        Jan bytes 548725 files 355 max 87710 name filename01 Feb bytes 24031383 files 527 max 11223344 name filename03
        Thank you greatly!
        Super Monker, please allow me to ask you for further advise of how can I count the uncompressed size of the files in each month in Julian order? As of if we know the ratio of the uncompressed file size is the file size * 2, it'll make the process much easier. Suppose add something in the code as below:
        (${$m[$day]}{'bytes'} += $value, *2${$m[$day]}{'files'})
        Or something as the likes...