in reply to Sort directory by file size
Hello nnigam1, and welcome to the Monastery!
I think the problem is that the -s file test returns undef when the file is empty (i.e., when it has a zero size). One way to fix this is to test for undef and change it to zero using the // (logical defined-or) operator (see perlop#Logical-Defined-Or):
#! perl use strict; use warnings; opendir D1, ...; print "$_\n" for map { sprintf qq[%s: %d], $_, -s $_ // 0 } sort { (-s $a // 0) <=> (-s $b // 0) } grep { ! -d } readdir D1;
Update: Marshall below is correct: -s returns undef only when the file does not exist. My problem was exactly as identified by choroba above: by failing to prepend the directory to the filename, I was calling -s on non-existent files. D’oh! :-(
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sort directory by file size
by Marshall (Canon) on May 18, 2016 at 16:29 UTC |