I have an HTML file consisting of an HTML table with links to Scientific Papers and Authors and with their year of publishing. The html is sorted from oldest to newest. I need to resort the table by parsing the file and getting a new file with the sorted source code from newest to oldest.
Here is a small perl script that should be doing the job but it produces semi-sorted results
local $/=undef;
open(FILE, "pubTable.html") or die "Couldn't open file: $!";
binmode FILE;
my $html = <FILE>;
open (OUTFILE, ">>sorted.html") || die "Can't oupen output file.\n";
map{print OUTFILE "<tr>$_->[0]</tr>"}
sort{$b->[1] <=> $a->[1]}
map{[$_, m|, +(\d{4}).*</a>|]}
$html =~ m|<tr>(.*?)</tr>|gs;
close (FILE);
close (OUTFILE);
And here is my input file:
link
and what I get as an output:
link
From the output you can see the order is going well but then I get the year 1993 after the year 1992 and not in the beginning of the list.
There was a problem with the regex in the
mapbecause of the following lines in the html.and
In the 1989 line the year includes a comma at the end and there’s no whitespace in front. Because of that, the script threw a lot of warnings and always put that line in the bottom.
The other three lines have a four-digit number
(\d{4})with something behind it.*(the year). So the sorting used the other numbers (7115, 2269, 1514) to sort and those were mixed up with the years.You need to adjust the regex accordingly to fix those issues.
Before:
After: