User:Ojw/SortTable
From Wikipedia, the free encyclopedia
< User:Ojw
#!/usr/bin/perl my $Filename = shift(); die("usage: $0 [filename] > [outputfile]\n") if(!-f $Filename); $Page = Slurp($Filename); if($Page =~ /(\{\|.*?|-)(.*?)(\s*\|\})/sm){ $Before = $` . $1; $After = $3 . $'; $Table = $2; %Rows2; @Rows = split(/\s*\|-\s*/, $Table); foreach $Row (@Rows){ @Fields = split(/\s*\|\s*/, $Row); $Name = lc($Fields[1]); $Name =~ s/^.*\[\[//; $Name =~ s/\]\].*$//; $Rows2{$Name} = $Row; } @Sorted; foreach $Key(sort keys %Rows2){ push @Sorted, $Rows2{$Key}; } die(print join("\n",sort keys %Rows2)) if(0); $Output = $Before. join("\n|-\n", @Sorted). $After; print $Output; } else{print "No table found";} sub Slurp(){ open(my $fp, "<", shift()) || die(); $Data = join("",<$fp>); close $fp; return $Data; }