use diagnostics; use File::Slurp; use RTF::Writer; my @destinations = ('IAs','Defs','Term','Payoffs','RWs','Boilerplate2'); my $targetfolder = "contract/new2"; opendir(DIR, "$targetfolder"); my @files = readdir(DIR); closedir(DIR); my $count = 0; foreach $file (@files) { if ($file =~ /rtf/i) { my %writer; for my $dest (@destinations) { $writer{$dest} = RTF::Writer->new_to_file("contract/$dest/$file"); } my $contract = read_file("$targetfolder/$file"); my @paragraphs = split('cf', $contract); foreach $clause (@paragraphs) { my @lines = split("\n", "$clause"); if ($lines[0] =~ /^\d$/){ my $colour = $lines[0]; my $goal = $writer{$destinations[$colour]}; #NO-LONGER-PROBLEMATIC CODE my $destinationfolder = ("contract/$destinations[$colour]"); # Also changed this one foreach $line (@lines) { if ($line =~ /^\s*\w/ && $line !~ /^\d$/){ $goal->paragraph ( "$line", ); } } } } } $count++; }