I have this part of a code for editing cue sheets and I don’t know how to reverse two consecutive lines if found:
/^TITLE.*?"$/
/^PERFORMER.*?"$/
to reverse to
/^PERFORMER.*?"$/
/^TITLE.*?"$/
What would it be the solution in my case?
use strict;
use warnings;
use File::Find;
use Tie::File;
my $dir_target = 'test';
find(\&c, $dir_target);
sub c {
/\.cue$/ or return;
my $fn = $File::Find::name;
tie my @lines, 'Tie::File', $fn or die "could not tie file: $!";
for (my $i = 0; $i < @lines; $i++) {
if ($lines[$i] =~ /^REM (DATE|GENRE|REPLAYGAIN).*?$/) {
splice(@lines, $i, 3);
}
if ($lines[$i] =~ /^\s+REPLAYGAIN.*?$/) {
splice(@lines, $i, 1);
}
}
untie @lines;
}
Well, since you’re tying into an array, I’d just check
$lines[$i]and$lines[$i+1](as long as the +1 address exists, that is), and if the former matches TITLE and the latter PERFORMER, swap them. Unless perhaps you need to transpose these even if they’re not consecutive??Here’s an option (this snippet would go inside your
forloop, perhaps above the REM-checking line) if you know they’ll be consecutive:Another option (which would work regardless of consecutiveness, and is arguably more elegant) would be to
(up at the top, with your other
usestatements) and then do (inside&c, but outside theforloop):Is that what you’re after?