我想使用File :: Find :: Rule从音乐结构目录中提取值,如果存在,我很难获得每张专辑的总光盘,例如,如果一个专辑包含3个子目录DISC1,DISC2,DISC3 - 总光盘值应为3.如果我在“For”语句之前grep这些目录,它将获得所有找到的总数,如果我在“For”语句中尝试,则一次计算一个。如果存在,如何提取每张专辑的总光盘。感谢。
use autodie;
use strict;
use warnings;
use File::Find::Rule;
use File::Spec;
my $dir = 'D:\Test';
$dir =~ s#\\#/#g;
my @fd = File::Find::Rule->directory()
->name( qr/\(\d+\)/ )
->in( $dir );
my $grep_totaldiscs = grep /DISC\d+/, @fd;
print "$grep_totaldiscs\n";
for my $fd ( @fd ) {
my ($genre, $artist, $album, $disc) = (File::Spec->splitdir($fd))[2..5];
my ($discnumber, $totaldiscs);
if ($fd =~ /DISC(\d+)/) {
$discnumber = $1;
$totaldiscs = $1 if ( defined($totaldiscs) < $1 );
print "$album $totaldiscs\n";
}
}
答案 0 :(得分:0)
概述:
我在这里使用了艺术家和专辑来消除散列键的歧义。
例如:
my %album_discs;
for my $fd ( @fd ) {
my ($genre, $artist, $album, $disc) = (File::Spec->splitdir($fd))[2..5];
my $aakey = "${artist}~${album}";
my ($discnumber, $totaldiscs);
if ($fd =~ /DISC(\d+)/) {
$discnumber = $1;
$album_discs{$aakey} = $discnumber
if ( ! exists $album_discs{$aakey} || $album_discs{$aakey} < $discnumber );
}
}
for my $aakey ( keys %album_discs ) {
my ( $artist, $album ) = split( /~/, $aakey );
print "Number of discs for album $album by $artist is $album_discs{$aakey}\n";
}