in reply to Re: Replace $a with $b if $a ne $b
in thread Replace $a with $b if $a ne $b

There are at least a few different (and sensible) ways to do what you want in that "foreach" loop. Personally, I'd opt for turning the list into a hash of arrays, so that it is structured in a way that is consistent with what the output should be:
my %dirhash; for my $file ( @filelist ) { my ( $dir, name ) = split '/', $file; pushd @{$dirhash{$dir}}, $name; } for my $dir ( sort keys %dirhash ) { print "$dir\t", join( "\n\t", @{$dirhash{$dir}} ), "\n"; }
Another way, more similar to your original idea, would be to keep track of what the last printed directory was, and only print the directory name when it changes from the previous:
my $lastdir = ''; for my $file ( @filelist ) { my ( $dir, $name ) = split '/', $file; if ( $dir ne $lastdir ) { print "$dir"; $lastdir = $dir; } print "\t$name\n"; }