#fix dir for my $i (@arr){ $i->{dir} = $1 if $i->{path} =~ m{^(.+)/}; } #### /c/ /c/a1.mp3 /c/a2.mp3 /c/aa/ /c/bb/ /c/bb/aa1.mp3 /c/bb/aa2.mp3 /c/cc/