#!/usr/bin/perl use strict; use warnings; use File::Find; use File::stat; use List::Util qw(sum min max); my %files; my $root_dir = '/root/Desktop/Recent'; my $min_file_size = 25000; &find_files; &create_and_print_output; sub find_files { print "\nTraversing tree $root_dir...\n\n"; find(\&trav_tree, "$root_dir" ); } sub trav_tree { if ( -e $File::Find::name ) { $files{ 'files' }->{ total } += 1; push @{ $files{ 'files' }->{ sizes } }, stat($File::Fin +d::name)->size; if ( stat($File::Find::name)->size >= $min_file_size ) +{ $files{ 'sizes' }->{ stat($File::Find::name)->si +ze }->{ filename } = $File::Find::name; } } } sub create_and_print_output { my $sum = sum( @{ $files{ 'files' }->{ sizes } } ); my $number_of_files = $files{ 'files' }->{ total }; my $average_file_size = $sum / $number_of_files; my $smallest_file_name = $files{ 'sizes' }->{ min ( keys %{ $ +files{ 'sizes' } } ) }->{ filename }; my $smallest_file_size = min ( keys %{ $files{ 'sizes' } } ); my $biggest_file_name = $files{ 'sizes' }->{ max ( keys %{ $f +iles{ 'sizes' } } ) }->{ filename }; my $biggest_file_size = max ( keys %{ $files{ 'sizes' } } ); printf "Number of files: %d\n", $number_of_files; printf "Average file size: %d bytes\n\n", $average_file_size; + printf "Closest to average file found was %s with a size of % +d bytes\n", $smallest_file_name, $smallest_file_size; print "\n"; }
In reply to Re: Creating a sub-list constrained by an average while maintaining maximum number of original elements
by Khen1950fx
in thread Creating a sub-list constrained by an average while maintaining maximum number of original elements
by Jack B. Nymbol
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |