Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

using the stat function

by Anonymous Monk
on Jun 12, 2002 at 12:41 UTC ( [id://173794]=perlquestion: print w/replies, xml ) Need Help??

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

opendir(USER, "$user{'site_id'}"); @userfiles = grep !/^\.\.?\z/, readdir USER; close USER; @userfiles=(sort @userfiles); foreach $line (@userfiles) { @stats = stat("$user{'site_id'}/$line"); }
now for $stats7 which gives you the size, I need to make it say KB at the end if its over 1000 and then if its not Bytes.

Replies are listed 'Best First'.
Re: using the stat function
by katgirl (Hermit) on Jun 12, 2002 at 12:51 UTC
    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.

      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.

      well now what if I want stat9 which is the time, and I just wanted to display the date.
Re: using the stat function
by broquaint (Abbot) on Jun 12, 2002 at 12:55 UTC
    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

Re: using the stat function
by gav^ (Curate) on Jun 12, 2002 at 13:05 UTC
    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^

      Thanks You Guys

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://173794]
Approved by little
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-04-24 10:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found