home Mail List
Info
Info
Meetings
Goals
Upcoming
Projects
FAQ
Security
Links

[Date Prev][Date Next] [Chronological] [Thread] [Top]

[NMLUG] Random Questions #28



Not bad... a little inefficient because it calls find several times. This 
4-line ruby-ism:

tot={}
tot.default=0
`find .`.split("\n").each{|i|tot[i.sub!("/[^/]*$","")]+=1 until !i.rindex("/")}
tot.sort{|a,b| b[1]<=>a[1]}.each {|i,j| print j.to_s+" "+i+"\n"}

does the trick too, though I suspect it could be made even shorter (I feel
like we're playing "Name That Tune"):

 % the first two lines can probably be omitted or at least combined-- is
there any way to tell Ruby "on-the-fly" that tot is a hash defaulting to
0?)

 % the i.sub! thing seems kludgey-- I'd prefer a nested iterator here (is
there a String."gindex"(x): show *all* positions of x in String?--
probably not?)

 % it would be really cool to do "/long/dir/name/here/filename".split and
then somehow generate the array ["/long", "/long/dir", "/long/dir/name",
"long/dir/name/here"] but I couldn't find an ultra-easy way to do this (a
sort of "cumulative sum" function-- from an array x, return the array:
x[0], x[0]+x[1], x[0]+x[1]+x[2], etc). If this could be done ultra-easily,
I suspect the whole script could become a one-liner or close. Of course,
it's not hard to program a cumulative sum function, just not ultra-easy
(at least not as far as I can tell)

And while I'm replying, some random stuff:

% Wesley, I would be interested in more thoughts on tcsh-caching, but I'm
not sure script is the way to go, especially since I run screen and even
screen-inside-screen (using "screen -m"). My original plan was to create a
"cache-command"  script so that when "cache-command 'complex-command'" was
run, cache-command would do something like:

echo complex-command | tcsh | tee /var/cache/tcsh/md5sum-of-complex-command

and "find-cache-command 'complex-command'" would simply see if
/var/cache/tcsh/md5sum-of-complex-command existed. The big problem here is
that I've have to remember to type "cache-command" in front of each
command I wanted cached. If tcsh can be forced to prefix commands with a
string, that might work (I'd have to tweak cache-command not to cache
certain commands, of course)-- or something along those lines (this still
isn't quite perfect because it starts up another instantiation of tcsh for
each cached command, which is inefficient)

% Rant: It's taken me a long time to break my habit of functional
programming (I did things like f(g(h(x))) instead of y=h(x);z=g(y);w=f(z))
because I was told it was "too complicated" and "too hard to understand"--
it's sort of annoying that Ruby is bringing this back (though, admittedly,
postfix functional notation is probably more efficient than infix
[generates fewer stack frames, I'm guessing])

% Yes, I realize I misspelled "World Wind" in my original post.

--
Sincerely, Sarang Gupta (_sarang_@sarangworld.com)
Backup Emails: saranghome@softhome.net, sarangorama@gmail.com

On Sun, 28 Aug 2005, Wesley J. Landaker wrote:

> Date: Sun, 28 Aug 2005 16:31:44 -0600
> From: Wesley J. Landaker <wjl@icecavern.net>
> Reply-To: New Mexico Linux Users Group Mail List <nmlug@nmlug.org>
> To: nmlug@nmlug.org
> Cc: Sarang Gupta <_sarang_@sarangworld.com>
> Subject: Re: [NMLUG] Random Questions #28
> 
> On Sunday 28 August 2005 15:28, Sarang Gupta wrote:
> > I believe that only works if there are no subdirectories? I'm looking for
> > output similar to [this is the structure of the Java docs directory,
> > sorted by number of files-- showing only the first few entries since this
> > is just an example]:
> >
> > 9524 .
> > 6918 ./api
> > 3097 ./api/javax
> > 2468 ./api/java
> > 2390 ./guide
> 
> Okay, you want something like:
> 
> find . -type d -exec sh -c 'echo -ne "{}: "; find {} | wc -l' \; | column -t
> 
> Enjoy.
> 
> -- 
> Wesley J. Landaker <wjl@icecavern.net>
> OpenPGP FP: 4135 2A3B 4726 ACC5 9094  0097 F0A9 8A4C 4CD6 E3D2
> 




Please send sugestions and comments to webmaster@nmlug.org.
Valid XHTML 1.1! Valid CSS! Powered by Debian Powered by Apache