Extracting Directories and Sub Directories and Counting

Discussion in 'Perl Misc' started by Ron Smith, Nov 2, 2004.

  1. Ron Smith

    Ron Smith Guest

    #!/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
    Ron Smith, Nov 2, 2004
    #1
    1. Advertising

  2. On 1 Nov 2004 20:48:39 -0800, (Ron Smith)
    wrote:

    >#!/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
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Nov 2, 2004
    #2
    1. Advertising

  3. Ron Smith <> wrote:


    > 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?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 2, 2004
    #3
  4. Ron Smith wrote:

    <code snipped>

    > 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 (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
    :

    http://www.mail-archive.com//msg63290.html

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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 2, 2004
    #4
  5. Michele Dondi wrote:

    > On 1 Nov 2004 20:48:39 -0800, (Ron Smith)
    > wrote:
    >
    >
    >>#!/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.
    Brian McCauley, Nov 2, 2004
    #5
  6. On Tue, 02 Nov 2004 18:08:26 +0000, Brian McCauley <>
    wrote:

    >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
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Nov 2, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joel Finkel
    Replies:
    0
    Views:
    491
    Joel Finkel
    Sep 12, 2003
  2. Ksenia Marasanova

    extracting HTML fragments and counting words

    Ksenia Marasanova, Feb 18, 2005, in forum: Python
    Replies:
    0
    Views:
    296
    Ksenia Marasanova
    Feb 18, 2005
  3. Ben
    Replies:
    2
    Views:
    886
  4. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,024
    Lawrence D'Oliveiro
    May 20, 2011
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    199
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page