<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
# The script is designated to order the references in the external .tex-file.
# It is assumed that all the references are introduced using standard \bibitem{key}. M.S.
if(!$ARGV[0]) 
{print "Enter the external .tex-file name in which You want to order references:";
$file=&lt;STDIN&gt;;chomp($file);$fileout="$file.tex";
print "By default the output will be placed to the file $fileout\nYou can specify the output using option -o in the command line call: ./Orderer filename -o OutputName\n";}
else {$file=$ARGV[0];if(($ARGV[1] eq '-o')and($ARGV[2])){$fileout=$ARGV[2]}else{$fileout="$file.tex"}};
print "Opening file $file\n";
open(SM,$file);@lines=&lt;SM&gt;;close(SM);
# Now drop all the lines which start with %(comment)
foreach $line(@lines){if($line!~/^[ ]*\%/){push(@Lines,"$line")}};
foreach $line(@Lines){if($line=~ /\\cite\{[a-zA-Z:0-9\,\']+\}/g){&amp;AddCite($&amp;);
while($'=~ /\\cite\{[a-zA-Z:0-9\,\']+\}/g) # Multiple citations on the same page
 {&amp;AddCite($&amp;)}}};
print "$#Cite citations found\n"; 
foreach $line(@Lines){$x=$line;if($x=~s/(\\bibitem\{)([a-zA-Z:0-9\,\']+)(\}\s)$/\2/g){$hash=$x;chomp($hash)};
if($hash){if($line!~/\\end/){@Y=("$BibItem{$hash}","$line");$BibItem{$hash}="@Y";}
  else{push(@LastLines,$line)}}else{push(@FirstLines,$line)};};
print "Printing out ordered list into file $fileout:\n";
open(SM,"&gt;$fileout");print SM "@FirstLines";
foreach $citation(@Cite){print SM $BibItem{$citation}};
print SM "@LastLines";close(SM);&amp;Check; 
sub AddCite
# The subroutine extracts the citation and adds it to the specified array
# Format of the call: (String)
{$string=$_[0];$string=~ s/^(\\cite\{)([a-zA-Z:0-9\,\']+)(\})$/\2/g;@Values=split(/\,/,$string);
foreach $value(@Values){if("@Cite"!~/$value/){push(@Cite,$value)};}};
sub Check
{@BibItemsList=keys %BibItem;
foreach $citation(@Cite){if("@BibItemsList"!~/$citation/){print "Warning::Citation $citation is not found !\n "}};
foreach $bib(@BibItemsList){if("@Cite"!~/$bib/){print "Warning::Bibitem", $BibItem{$bib}, " is not used !\n "}};};</pre></body></html>