Well, I'm not very good, but:
if ($stat[7] <1000) { print "$stat[7] bytes" }
else{ print $stat[7]/1000 ." KB" }
does that work? I don't think there's 1000 bytes in a KB though, but I can't remember the exact number. | [reply] [d/l] |
This has actually started to become a source of confusion. In data processing, traditionally the kilo-, mega-, giga-, tera-, etc., extensions were powers of 1024, not 1000 (1, 2, 3, 4, etc., respectively), although recently there seems to be a push to force these to be used in the SI (metric) respect of being powers of 1000. This results in a difference of 24 bytes per K, 48,576 bytes per M, 73,741,824 bytes per G, etc.
According to the IEC International Standard definitions (1998), the units we traditionally think of KB, MB, GB, TB, etc., are now technically KiB, MiB, GiB, TiB, etc.
So the number you were thinking was 1024, although you managed to be correct both in actual number you suggested, and in your thought about another value.
| [reply] |
well now what if I want stat9 which is the time, and I just wanted to display the date.
| [reply] |
I need to make it say KB at the end if its over 1000 and then if its not Bytes
You may also want your code to work as you re-assign @stats in every iteration of the foreach loop. Here's a modification of your code to nudge you in the right direction
foreach my $line (@userfiles) {
my $size = (stat("$user{'site_id'}/$line"))[7];
my $readable;
if($size >= 1024) {
$readable = sprintf("%2.2fKB", $size / 1024);
} else {
$readable = $size . "Bytes";
}
print "$line size is: $readable", $/;
}
HTH
_________ broquaint | [reply] [d/l] |
This should probably do the trick:
opendir(DIR, $user{site_id})
|| die "Unable to open directory $user{site_id}: $!";
my @userfiles = sort grep !/^\.\.?\z/, readdir DIR;
close DIR;
foreach my $file (@userfiles) {
my $size = format_size(-s "$user{site_id}/$file");
print $size, "\n";
}
sub format_size {
my $bytes = shift;
if ($bytes >= 1048576) {
return sprintf("%.2f MB", $bytes / 1048576);
} elsif ($bytes >= 1024) {
return sprintf("%.2f KB", $bytes / 1024);
} else {
return "$bytes Bytes";
}
}
gav^
| [reply] [d/l] |