Extracting Directories and Sub Directories and Counting

R

Ron Smith

#!/usr/bin/perl

use strict;

print "\n";
my %HoA;
for ( `dir /b/s` ) {
push @{ $HoA{$1} }, $2 if /(.+)\\(\w+)\.\d+\.\w+$/;
}

for my $dir ( sort keys %HoA ) {
print join ( "\n", $dir ), "\n\n";
my @basenames = @{ $HoA{$dir} };
my %count;
for my $frames ( @basenames ) {
$count{$frames} += 1;
}

for ( sort keys %count ) {
printf "%30s\t%04d\n", $_, $count{$_};
}
print "\n";
}

gives me:

file_base_name file_count

in two columns. How would I add additional columns like:

file_base_name file_count File_extension file_size

Which construct would I use? Would it be a 'HoH', or simply expand on
a 'HoA', or is it another construct like 'AoA' or AoH?

Any help would be appreciated.

TIA
Ron
 
M

Michele Dondi

#!/usr/bin/perl
[snip code]

Please don't take my words as a personal attack, but this made my
heart really hurt. I didn't even try to understand your code. What do
you really want to do? Maybe someone may suggest more effective ways
to do it.

Also, the 'dir /b/s' bit suggests you may be interested in File::Find,
anyway.


Michele
 
T

Tad McClellan

print join ( "\n", $dir ), "\n\n";

That statement is equivalent to this one:

print "$dir\n\n";

i.e. the join() doesn't do anything.

What were you hoping that join() would do for you there?
 
G

Gunnar Hjalmarsson

Ron Smith wrote:

gives me:

file_base_name file_count

in two columns. How would I add additional columns like:

file_base_name file_count File_extension file_size

Which construct would I use? Would it be a 'HoH', or simply expand on
a 'HoA', or is it another construct like 'AoA' or AoH?

I was very disappointed to see this post here, Ron. First of all, since
you posted basically the same question to (e-mail address removed) (where I
have answered it, btw), what you did is called multi-posting and is
considered rude. See why here:

http://www.uwasa.fi/~ts/http/crospost.html

Furthermore, your question was preceded by a long thread at
(e-mail address removed):

http://www.mail-archive.com/[email protected]/msg63290.html

Do you really believe that you explained your problem properly to those
who have not read the previous posts?
 
B

Brian McCauley

Michele said:
#!/usr/bin/perl

[snip code]

Please don't take my words as a personal attack, but this made my
heart really hurt. I didn't even try to understand your code. What do
you really want to do? Maybe someone may suggest more effective ways
to do it.

Also, the 'dir /b/s' bit suggests you may be interested in File::Find,
anyway.

I have found that File::Find can be slow on Win32 (because, IIRC, stat()
is rediculously slow[1]). For most things this is not enough to stop me
using File::Find but occasionally for really big trees I've resorted to
parsing the output of 'dir /b /s'.

[1] I'm not sure this is still the case as I'm talking about something I
encountered many years ago.
 
M

Michele Dondi

I have found that File::Find can be slow on Win32 (because, IIRC, stat()
is rediculously slow[1]). For most things this is not enough to stop me
using File::Find but occasionally for really big trees I've resorted to

Well, in my personal experience it is fast enough that I don't notice
it to be slow. But then I carry on most of my file management
activities under Linux, even on Win* FS's.

For the record I've tried this both under Windows98 (sorry, i.e.
happy: no XP yet!) and Linux (kernel 2.6.9):


#!/usr/bin/perl -l

use strict;
use warnings;
use Time::HiRes 'time';

my ($n,$t)=(250_000,time);
stat $ARGV[0] while $n--;
print time - $t;

__END__


As a rough indication I get these figures (no claim of real accuracy):

(*) Windows98

32s both with files in CWD and with absolute paths,

(*) Linux

vfat[1] -> 0.34s with files in CWD (relative path),
0.43s with files "involving two dirs"[2],

ReiserFS & tmpfs -> 0.26s with files in CWD (relative path),
0.38s with files "involving two dirs".
parsing the output of 'dir /b /s'.

OTOH *on my system* (and I do not doubt that it is a quirk of it)
starting a shell by whichever means (no matter if via C<qx/.../>,
'-|'-open()s or whatever) involves some "mysterious" and annoying
floppy disk activity. This is not by any means limited to perl and
happens also, e.g. with my text editor in shell interaction mode[3].


[1] To be precise, exactly the same file(s) also tested under Windows.
[2] I mean: whose full path is of the kind of /mnt/winc/test.txt.
[3] Irritatingly enough, this behaviour is intermittent.


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,432
Latest member
GTRNorbert

Latest Threads

Top