Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Golfing a size/date grabber

by delirium (Chaplain)
on Nov 19, 2003 at 18:50 UTC ( #308378=perlquestion: print w/replies, xml ) Need Help??

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

Hey guys, I have a command line I've been using to total the number of bytes in files, sorting by the day they were last modified. It takes piped input from a find command, that I in turn pipe into sort to get an easy to read list, e.g., find ./ -type f | perl ..perl stuff.. | sort

Here's the code I've been using:

perl -MData::Dumper -lne '@f=localtime((stat)[9]);$t{sprintf "%d%02d%0 +2d",$f[5]+1900,$f[4]+1,$f[3]}+=(stat)[7];END{print Dumper \%t}'

The goal is to simply print a list of dates and the total size of files modified on that day. Any output style that is readable and has the correct date in YYYYMMDD format is fine.

How can I shorten this a little?

Update: Thanks for everyone's input. There were a couple of comments that indicated that this may have been pretty pointless. I wasn't trying to improve flawed code or squeeze an extra 2% efficiency out of code that only runs once a week.

I was, however, looking for a few new things to add to the bag of tricks. I didn't know that END{} could be replaced by a simple}{, nor did I have any occasion to use the -s test switch for file sizes. I knew there must be a way to use either qw/1900 01 00/ or "19000100" to squeeze off a few more characters, but I couldn't see the solution (although hindsight is 20/20). Here's the code I ended up with, module-free and 40-odd characters squeezed off of the original command line, which originally included a find . -type f, and a final pipe to sort:

find .|perl -lne'$t{19000100+sprintf"%02d"x3,(localtime((stat)[9]))[5, +4,3]}+=-s if!-d}{print"$_ $t{$_}"for sort keys%t'

Replies are listed 'Best First'.
Re: Golfing a size/date grabber
by sauoq (Abbot) on Nov 19, 2003 at 19:45 UTC

    This shortens it a little and cleans up the output some too.

    perl -lne '@f=localtime((stat)[9]);$t{sprintf"%d%02d%02d",$f[5]+1900,$ +f[4]+1,$f[3]}+=-s;}{print"$_ $t{$_}"for keys%t'

    But... to what purpose do you want to golf this? If you are tired of typing it or something, just stick it in a shell script as is and forget about it.

    -sauoq
    "My two cents aren't worth a dime.";
    

      And that's only a bit shorter (and a bit faster) but still pointless to golf this

      perl -lne '@f=localtime((stat)[9]);$t{sprintf'%02d'x3,$f[5]+1900,$f[4]+1,$f[3]}+=-s _;}{print"$_ $t{$_}"for keys%t'

      BTW: sauoq got ++

      to what purpose do you want to golf this?

      Asthetics, mainly. I was never happy with having the two stat commands in there, and the END{} block was ugly, and I didn't really want to import Data::Dumper.

      Fortunately, all these problems have been solved. Thanks.

Re: Golfing a size/date grabber
by Zed_Lopez (Chaplain) on Nov 19, 2003 at 19:59 UTC

    Following your basic strategy, but using -s, Time::Piece and YAML:

    perl -MTime::Piece -MYAML -nle '$t=localtime+(stat)[9];$d{$t->ymd}+=-s;END{print Dump \%d}'
Re: Golfing a size/date grabber
by !1 (Hermit) on Nov 19, 2003 at 20:09 UTC
    perl -MYAML -pe'$t{19000100+sprintf"%d%02d%02d",(localtime((stat)[9])) +[5,4,3]}+=-s}{$_=Dump\%t'

    Well, that was fun.

    Thanks to Roy Johnson for pointing out a goof.

Re: Golfing a size/date grabber
by Anonymous Monk on Nov 19, 2003 at 19:42 UTC

    Here you go. A smidgen shorter than your whole command line, but all within Perl:

    perl -MPOSIX=strftime -MFile::Find -e'find(sub{-f and$t{strftime"%Y%m% +d",localtime((stat)[9])}+=-s;},".");print"$_ => $t{$_}\n"for sort key +s%t;'
Re: Golfing a size/date grabber
by Roy Johnson (Monsignor) on Nov 19, 2003 at 19:38 UTC
    (stat)[9] to -M
    (stat)[7] to -s
    Update: This bit someone else, too, so I'll mention it: not only is -M relative to the current time, it is in days, not in seconds, so to get (stat)[9], you need to multiply it by 86500 and add it to time. No saving keystrokes that way.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2021-10-21 10:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (83 votes). Check out past polls.

    Notices?