[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Hoaky MacPerl Regular Expression Question



On Thu, May 13, 1999 at 10:16:43AM -0700, Chilton Webb wrote:
> I've been knocking this one around in my miniature brain for about a 
> month now, so I guess I'm not going to figure it out on my own.
> 
> I have a list of files, with this structure:
> 
> ------
> Macintosh HD:myFiles:a.txt
> Macintosh HD:myFiles:b.txt
> Macintosh HD:myFiles:more:a.txt
> Macintosh HD:myFiles:more:b.txt
> Macintosh HD:myFiles:more:yetmore:a.txt
> Macintosh HD:myFiles:more:yetmore:b.txt
> Macintosh HD:yurFiles:a.txt
> Macintosh HD:yurFiles:b.txt
> ------
> 
> And I want them in this format:
> 
> ------
> Macintosh HD
> 
>   myFiles
>     a.txt
>     b.txt
> 
>     more
>       a.txt
>       b.txt
> 
>       yetmore
>         a.txt
>         b.txt
> 
>   yurFiles
>     a.txt
>     b.txt
> 
> ------
> 

Here's something I just hacked together....

This assumes that the files are already in a list, and that the list is
already sorted.  Also, it's hard-coded for : as the directory separator.

If your script is building the file list by traversing the directories
anyway, it would be cleaner to format the output at the same time.


#!/usr/local/bin/perl

chomp(@filelist = <DATA>);

format_filelist('', 0, [@filelist]);

sub format_filelist {
    my($path, $indent, $listref) = @_;

    my($file, $full, $next);

    while (@$listref) {
        $file = $ {$listref}[0];
        if (not ($full, $next) = $file =~ /^(\Q$path\E([^:]+):?)/) {
            return;
        } elsif ($full =~ /:$/) {
            print "\n", ' ' x $indent, $next, "\n";
            format_filelist($full, $indent + 2, $listref);
        } else {
            print ' ' x $indent, $next, "\n";
            shift @$listref;
        }
    }
}
__END__
Macintosh HD:myFiles:a.txt
Macintosh HD:myFiles:b.txt
Macintosh HD:myFiles:more:a.txt
Macintosh HD:myFiles:more:b.txt
Macintosh HD:myFiles:more:yetmore:a.txt
Macintosh HD:myFiles:more:yetmore:b.txt
Macintosh HD:yurFiles:a.txt
Macintosh HD:yurFiles:b.txt

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org