I am not too sure exactly where to put this, as it is mainly a regex question but slightly crosses over into perl syntax.
Hopefully all will become clear.
At the moment, I have a directory full of files called
chapter1.txt, chapter2.txt and so on. Within each of these files are references encased in square brackets which I am trying
to link to external files. The format of the link is c1f1.html for chapter 1 reference 1, c3f5.html for chapter 3 reference 5.
So, in chapter 1,
[1]
becomes <a href="c1f1.html">[1]</a> and so on.
I have come up with a bit of code below
Code: Select all
opendir (DIR, "/home/paul/work/") or die "$!";
my @files = grep {/chapter*txt/} readdir DIR;
foreach my $file (@files)
{
open(FH,"/home/paul/work/$file") or die "$!";
my ($chapnumber) = ($file =~/chapter(\d+).txt/);
while (<FH>)
{
$dummyvar = ~s/\[(\d+\)]/<a href=\"c.$chapnumber.f.$1\.html\">\[$1\]<\/a>/g;
}
close(FH);
}
As far as I can see I'm extracting the chapter number from the title correctly, and the regex for replacing within
the file looks OK.
Can someone please suggest what might be wrong?
Many thanks
Paul