in reply to Re: Replace $a with $b if $a ne $b
in thread Replace $a with $b if $a ne $b
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 %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"; }
my $lastdir = ''; for my $file ( @filelist ) { my ( $dir, $name ) = split '/', $file; if ( $dir ne $lastdir ) { print "$dir"; $lastdir = $dir; } print "\t$name\n"; }
|
|---|