[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 07:14:46PM +0000, Richard K. Moore wrote:
> 
> #!/usr/bin/perl -w
> use diagnostics;
> use strict;
> 
> 
> my @Files =
> (
>     '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'
> ) ;
> 
> my $CurDir = '' ;
> 
> foreach my $File (@Files)
> {
>     my $Depth = ( $File =~ s/:/:/g ) - 1 ;

tr/:// is the preferred way to count characters.

>     my ( $NewDir, $Base )   = $File =~ /.*?:([^:]*):([^:]*)$/g ;

.*? serves no useful purpose here.

>     if ( $NewDir ne $CurDir )
>     {
>         print "\n", "  " x $Depth, "$NewDir\n" ;
>         $CurDir = $NewDir
>     } ;
> 
>     print "  " x ( $Depth + 1 ), "$Base\n" ;
> }
> 
> # Output:
> #
> #   myFiles
> #     a.txt
> #     b.txt

You're missing 'Macintosh HD'.  In particular, this code only outputs the
last directory name before the file; the rest of the path is ignored.
(i.e. @Files = ('HD:F1:F2:F3:file') would print simply:

      F3
        file
)  

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