I doubt that you will actually need "the most efficient way" which would mean some sort of performance objective to me. My advice is to do the job in a way that you can understand. All of the Perl ways will run quickly. Worry about efficient after you get something that works and that you understand how it works.
I'm not sure what the indentation level is exactly. Could it be that this "Eminem" token should be deleted it it is there?
use strict;
use warnings;
use Data::Dumper;
my @cases=(
'/Volumes/WD/Not Migrating/Music/Ana Tijoux (An Artist)',
'/Volumes/WD/Not Migrating/Music/Eminem/Ana Tijoux/Luchin (An Album)',
'/Volumes/WD/Not Migrating/Music/Eminem/Ana Tijoux/Luchin/Luchin.m4a (
+A Song)');
foreach my $case (@cases)
{
$case =~ s|^.+Music/||; # remove /Volumes/WD/Not Migrating/Music/
$case =~ s|\(.+\)||; # remove trailing (An Artist), etc.
print "\nLooking at case:$case...\n";
my @parts = split ('/',$case);
my $indent = 0;
if (@parts == 3){$indent=1;}
elsif (@parts==4) {$indent=2;}
print "# parts=".@parts," , the indent level=$indent\n";
print "indent line is next:\n";
print "\t"x$indent,"$parts[-1]\n"; #use only last part?? and indent
+??
print "all parts:\n";
print Dumper \@parts;
}
__END__
PRINTOUT:
Looking at case:Ana Tijoux ...
# parts=1 , the indent level=0
indent line is next:
Ana Tijoux
all parts:
$VAR1 = [
'Ana Tijoux '
];
Looking at case:Eminem/Ana Tijoux/Luchin ...
# parts=3 , the indent level=1
indent line is next:
Luchin
all parts:
$VAR1 = [
'Eminem',
'Ana Tijoux',
'Luchin '
];
Looking at case:Eminem/Ana Tijoux/Luchin/Luchin.m4a ...
# parts=4 , the indent level=2
indent line is next:
Luchin.m4a
all parts:
$VAR1 = [
'Eminem',
'Ana Tijoux',
'Luchin',
'Luchin.m4a '
];