[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 12:47:57PM -0400, Chris Nandor wrote:
> Well, without the extra linebreaks you want, I came up with:
> 
> [snip]

That code contained a bug.  Try it with the following file list:

my @list = (
  'Macintosh HD:myFiles:more:a.txt',
  'Macintosh HD:myFiles:more:b.txt',
  'Macintosh HD:yurFiles:more:a.txt',
  'Macintosh HD:yurFiles:more:b.txt',
);

Output:

Macintosh HD
  myFiles
    more
      a.txt
      b.txt
  yurFiles
      a.txt
      b.txt


The directory 'more' is not printed for yurFiles, because @last wasn't
reset for the subdirectories when switching from 'myFiles' to 'yurFiles'.


This should fix the bug:

#!perl
use File::Basename;
use strict;
use vars qw(@last);

my @list = (
  'Macintosh HD:myFiles:more:a.txt',
  'Macintosh HD:myFiles:more:b.txt',
  'Macintosh HD:yurFiles:more:a.txt',
  'Macintosh HD:yurFiles:more:b.txt',
);
  
foreach (sort @list) {
  my @path = split /:/;
  my $dir = dirname($_);
  
  for (0 .. $#path) {
    next if defined($last[$_]) && $path[$_] eq $last[$_];
    $last[$_] = $path[$_];
    $#last = $_;  # clear subdirectories
    print " " x ($_*2), $path[$_], "\n";
  }
}
__END__

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