# ---End of remarks for corrections----------------------------------------

# Version 1.3.1 (April 13, 2012)
# Works on EconLit output generated January 2011 or later (from AEA EconLit: http://www.aeaweb.org/econlit)
#
# This Perl 5 script was originally written by 
# Marc Muendler and Tiago Ribeiro
# University of California, San Diego and Berkeley, Departments of Economics
# e-mails:  muendler@econ.ucsd.edu            tribeiro@econ.berkeley.edu
# URL:  http://econ.ucsd.edu/muendler/   
# The script is being maintained by Marc Muendler
# 
# Copyright (c) 2001 all rights reserved.
# 
# Copying of this file is authorized only if either
#  (1) you make _no_ changes to your copy, or
#  (2) if you do make any change, you name the altered file differently.
#      (You may keep the file name if you make changes only to 
#       sections that are surrounded by #--- separators.)
#
# Please report bugs (and suggested fixes, if any) to the authors.
# Note: There are two types of inactive print commands in the code:
#            "# print OUT", and 
#            "# @RM[$i]= ", the latter for output in note={} in the BibTeX file.
#       These commands may facilitate debugging when activated.
#

#-----------------------------------------------
# PLEASE ENTER OPTIONS AND FILENAME HERE as $cmd
$cmd = '-e -f -k -v download';
#-----------------------------------------------

# $cmd Command interpretation
$file= $cmd; $file =~ s/\-\w+\s+//g; 
$ext = $file; $ext =~ s/([\s\w\d\-\.]+)\.//g;
if($ext =~ m/\w+/){$ext = 'dat';}
$file=~ s/\.([\s\w\d\-]+)//;
$option = $cmd; $option =~ s/(\s+)*$file(\s+)*//g; $option =~ s/(\s+)*\.(\s+)*$ext//g;

#-----------------------------------------------------------------------------------------------------------
# CHANGE PATH NAMES for input and output files HERE
open(IN,"<c:/temp/$file.$ext")   or die "Can't open input file $file.$ext. Error ";
open(OUT,">c:/temp/$file.bib")   or die "Can't open output file $file.bib. Error ";
#-----------------------------------------------------------------------------------------------------------

$option =~ tr/A-Z/a-z/;
$option =~ s/\-[a-dg-jn-prsuw-z]//g; 
if($option =~ m/\-l/ && $option =~ m/\-k/){$option =~ s/\s+\-l/ /g;}
$option =~ s/\s\s/ /g;
if($option !~ m/\w+/){$optplot = "<none>"}
                else {$optplot=$option}
print OUT '% Input file: '. $file.'.'.$ext . "\n" ;
print OUT '% Options were: '. $optplot . "\n" ;
print OUT '%  -e: Transforms "et al." string into "{et al.}" in author=/editor items (default: no transformation)'. "\n" ;
print OUT '%  -f: Adds a File reference item (set to F) to each BibTeX entry (default: no file reference)'. "\n" ;
print OUT '%  -k: Creates BibTeX Keys from combination of author=/editor= and year= (default: running number; -k overrides -l)'. "\n" ;
print OUT '%  -l: Leaves BibTeX keys out and replaces them by a dummy that causes a BibTeX error (default: running number)'. "\n" ;
print OUT '%  -m: Does _not_ transform abbreviated Month names to full names---i.e. _no_ change of "Jan" to "January" etc. (default: transformation)'. "\n" ;
print OUT '%  -q: Changes double quotes to single quotes in title= and booktitle= (default: keep double quotes from AEA EconLit)'. "\n" ;
print OUT '%  -t: Creates working paper output as TECHREPORT and not as ARTICLE (default: ARTICLE)'. "\n" ;
print OUT '%  -v: Uses volume instead of number for BOOKS and INCOLLECTION items whenever both provided by AEA EconLit (default: number)'. "\n" ;
# print OUT '%  (Any further instruction was ignored.)'. "\n" ;
print OUT "\n" ;

$count=0;
$probljo=0;
$problwp=0;
$problbk=0;
$problph=0;
$problcl=0;
$noprbdt=0;

while(<IN>){

    $_ =~ s/&/\\&/g;          #Replace & for LaTeX;
    $_ =~ s/R\\&D/{R\\&D}/g;  #Replace R&D for LaTeX;
    $_ =~ s/AR\(/{AR}(/g;     #Replace AR(n) for LaTeX;
    $_ =~ s/MA\(/{MA}(/g;     #Replace MA(n) for LaTeX;

    $_ =~ s/\,\-Jr/ Jr./g;  #Replace "," in front of Jr. for BibTeX;
    $_ =~ s/\,\s+Jr/ Jr./g; #Replace "," in front of Jr. for BibTeX;

    $_ =~ s/\s+\'/ \`/g;     #Replace correct quotation mark for LaTeX;
    $_ =~ s/\s+\"/ \`\`/g;   #Replace correct quotation mark for LaTeX;
    $_ =~ s/\"\s/\'\' /g;    #Replace correct quotation mark for LaTeX;
    $_ =~ s/\"\:/\'\':/g;    #Replace correct quotation mark for LaTeX;
    $_ =~ s/\"\./\'\'./g;    #Replace correct quotation mark for LaTeX;
    $_ =~ s/\.\"/\'\'/g;     #Replace correct quotation mark for LaTeX;
    $_ =~ s/\"\,/\'\',/g;    #Replace correct quotation mark for LaTeX;
    $_ =~ s/\,\"/\'\'/g;     #Replace correct quotation mark for LaTeX;
    $_ =~ s/\"\;/\'\';/g;    #Replace correct quotation mark for LaTeX;
    $_ =~ s/\;\"/\'\'/g;     #Replace correct quotation mark for LaTeX;
    $_ =~ s/\"\!/\'\'!/g;    #Replace correct quotation mark for LaTeX;
    $_ =~ s/\"\?/\'\'?/g;    #Replace correct quotation mark for LaTeX;
    $_ =~ s/\"\-\-/\'\'--/g; #Replace correct quotation mark for LaTeX;

    $_ =~ s/\s+\.\s+\.\s+\.\s+/\$\\ldots\$ /g; #Replace triple dots for LaTeX;

    $_ =~ s/Africa/{A}frica/g; #Replace for LaTeX;
    $_ =~ s/America/{A}merica/g; #Replace for LaTeX;
    $_ =~ s/Asia/{A}sia/g; #Replace for LaTeX;
    $_ =~ s/Australia/{A}ustralia/g; #Replace for LaTeX;
    $_ =~ s/Europe/{E}urope/g; #Replace for LaTeX;
    $_ =~ s/Caribbean/{C}aribbean/g; #Replace for LaTeX;
    $_ =~ s/Latin/{L}atin/g; #Replace for LaTeX;
    $_ =~ s/Atlantic/{A}tlantic/g; #Replace for LaTeX;
    $_ =~ s/Pacific/{P}acific/g; #Replace for LaTeX;
    $_ =~ s/Indian/{I}ndian/g; #Replace for LaTeX;
    $_ =~ s/Baltic/{B}altic/g; #Replace for LaTeX;
    $_ =~ s/Sahara/{S}ahara/g; #Replace for LaTeX;
    $_ =~ s/EU/{EU}/g; #Replace for LaTeX;
    $_ =~ s/NAFTA/{NAFTA}/g; #Replace for LaTeX;
    $_ =~ s/MERCOSUL/{MERCOSUL}/g; #Replace for LaTeX;
    $_ =~ s/MERCOSUR/{MERCOSUR}/g; #Replace for LaTeX;
    $_ =~ s/NATO/{NATO}/g; #Replace for LaTeX;
    $_ =~ s/United\s+Nations/{U}nited {N}ations/g; #Replace for LaTeX;
    $_ =~ s/North/{N}orth/g; #Replace for LaTeX;
    $_ =~ s/South/{S}outh/g; #Replace for LaTeX;
    $_ =~ s/West/{W}est/g; #Replace for LaTeX;
    $_ =~ s/East/{E}ast/g; #Replace for LaTeX;

    $_ =~ s/Argentin/{A}rgentin/g; #Replace for LaTeX;
    $_ =~ s/Austria/{A}ustria/g; #Replace for LaTeX;
    $_ =~ s/Brazil/{B}razil/g; #Replace for LaTeX;
    $_ =~ s/British/{B}ritish/g; #Replace for LaTeX;
    $_ =~ s/Bulgaria/{B}ulgaria/g; #Replace for LaTeX;
    $_ =~ s/Canad/{C}anad/g; #Replace for LaTeX;
    $_ =~ s/Colombia/{C}olombia/g; #Replace for LaTeX;
    $_ =~ s/Costa\s+Rica/{C}osta {R}ica/g; #Replace for LaTeX;
    $_ =~ s/Chile/{C}hile/g; #Replace for LaTeX;
    $_ =~ s/Chinese/{C}hinese/g; #Replace for LaTeX;
    $_ =~ s/China/{C}hina/g; #Replace for LaTeX;
    $_ =~ s/Cuba/{C}uba/g; #Replace for LaTeX;
    $_ =~ s/Danish/{D}anish/g; #Replace for LaTeX;
    $_ =~ s/Denmark/{D}enmark/g; #Replace for LaTeX;
    $_ =~ s/Dutch/{D}utch/g; #Replace for LaTeX;
    $_ =~ s/Egypt/{E}gypt/g; #Replace for LaTeX;
    $_ =~ s/English/{E}nglish/g; #Replace for LaTeX;
    $_ =~ s/England/{E}ngland/g; #Replace for LaTeX;
    $_ =~ s/French/{F}rench/g; #Replace for LaTeX;
    $_ =~ s/France/{F}rance/g; #Replace for LaTeX;
    $_ =~ s/Finland/{F}inland/g; #Replace for LaTeX;
    $_ =~ s/Finnish/{F}innish/g; #Replace for LaTeX;
    $_ =~ s/German/{G}erman/g; #Replace for LaTeX;
    $_ =~ s/Great Britain/{G}reat {B}ritain/g; #Replace for LaTeX;
    $_ =~ s/Hebrew/{H}ebrew/g; #Replace for LaTeX;
    $_ =~ s/Holland/{H}olland/g; #Replace for LaTeX;
    $_ =~ s/India/{I}ndia/g; #Replace for LaTeX;
    $_ =~ s/Irish/{I}rish/g; #Replace for LaTeX;
    $_ =~ s/Ireland/{I}reland/g; #Replace for LaTeX;
    $_ =~ s/Israel/{I}srael/g; #Replace for LaTeX;
    $_ =~ s/Italian/{I}talian/g; #Replace for LaTeX;
    $_ =~ s/Italy/{I}taly/g; #Replace for LaTeX;
    $_ =~ s/Japan/{J}apan/g; #Replace for LaTeX;
    $_ =~ s/Jew/{J}ew/g; #Replace for LaTeX;
    $_ =~ s/Korea/{K}orea/g; #Replace for LaTeX;
    $_ =~ s/Mexic/{M}exic/g; #Replace for LaTeX;
    $_ =~ s/Morocc/{M}orocc/g; #Replace for LaTeX;
    $_ =~ s/Netherlands/{N}etherlands/g; #Replace for LaTeX;
    $_ =~ s/Niger/{N}iger/g; #Replace for LaTeX;
    $_ =~ s/Norwegian/{N}orwegian/g; #Replace for LaTeX;
    $_ =~ s/Norway/{N}orway/g; #Replace for LaTeX;
    $_ =~ s/Poland/{P}oland/g; #Replace for LaTeX;
    $_ =~ s/Portug/{P}ortug/g; #Replace for LaTeX;
    $_ =~ s/Russia/{R}ussia/g; #Replace for LaTeX;
    $_ =~ s/Scottish/{S}cottish/g; #Replace for LaTeX;
    $_ =~ s/Scotland/{S}cotland/g; #Replace for LaTeX;
    $_ =~ s/Spaniard/{S}paniard/g; #Replace for LaTeX;
    $_ =~ s/Spanish/{S}panish/g; #Replace for LaTeX;
    $_ =~ s/Spain/{S}pain/g; #Replace for LaTeX;
    $_ =~ s/Swedish/{S}wedish/g; #Replace for LaTeX;
    $_ =~ s/Swede/{S}wede/g; #Replace for LaTeX;
    $_ =~ s/Taiwan/{T}aiwan/g; #Replace for LaTeX;
    $_ =~ s/Tunisia/{T}unisia/g; #Replace for LaTeX;
    $_ =~ s/UK/{UK}/g; #Replace UK for LaTeX;
    $_ =~ s/U\.K\./{UK}/g; #Replace UK for LaTeX;
    $_ =~ s/United States/{U}nited {S}tates/g; #Replace US for LaTeX;
    $_ =~ s/US/{US}/g; #Replace US for LaTeX;
    $_ =~ s/U\.S\./{US}/g; #Replace U.S. for LaTeX;
    $_ =~ s/Venezuela/{V}enezuela/g; #Replace for LaTeX;

    $_ =~ s/Calif\./{CA}/g;
    $_ =~ s/Conn\./{CT}/g;
    $_ =~ s/D\.C\./{DC}/g;
    $_ =~ s/\s+DC/ {DC}/g;
    $_ =~ s/Mass\./{MA}/g;
    $_ =~ s/Md\./{MD}/g;
    $_ =~ s/Mich\./{MI}/g;
    $_ =~ s/N\.C\./{NC}/g;
    $_ =~ s/N\.D\./{ND}/g;
    $_ =~ s/N\.H\./{NH}/g;
    $_ =~ s/N\.J\./{NJ}/g;
    $_ =~ s/N\.M\./{NM}/g;
    $_ =~ s/N\.Y\./{NY}/g;
    $_ =~ s/Pa\./{PA}/g;
    $_ =~ s/R\.I\./{RI}/g;
    $_ =~ s/S\.C\./{SC}/g;
    $_ =~ s/S\.D\./{SD}/g;
    $_ =~ s/Va\./{VA}/g;
    $_ =~ s/Vt\./{VT}/g;
    $_ =~ s/W\.V\./{WV}/g;
    $_ =~ s/Wisc\./{WI}/g;

    $_ =~ s/CPI/{CPI}/g;     #Replace CPI for LaTeX;
    $_ =~ s/PPI/{PPI}/g;     #Replace PPI for LaTeX;
    $_ =~ s/WPI/{WPI}/g;     #Replace WPI for LaTeX;
    $_ =~ s/GDP/{GDP}/g;     #Replace GDP for LaTeX;
    $_ =~ s/GNP/{GNP}/g;     #Replace GNP for LaTeX;
    $_ =~ s/National Bureau of Economic Research, Inc, NBER/NBER/g; #Replace for convenience, and mark capital letters for LaTeX and abbreviate;
    $_ =~ s/National Bureau of Economic Research/NBER/g; #Replace for convenience, and mark capital letters for LaTeX and abbreviate;
    $_ =~ s/NBER/{NBER}/g;   #Replace capital letters for LaTeX and abbreviate;
    $_ =~ s/LSE/{LSE}/g;   #Replace capital letters for LaTeX and abbreviate;
    $_ =~ s/MIT/{MIT}/g;   #Replace capital letters for LaTeX and abbreviate;
    $_ =~ s/C.E.P.R. Discussion Papers, CEPR/CEPR/g; #Replace for convenience, and mark capital letters for LaTeX and abbreviate;
    $_ =~ s/CEPR/{CEPR}/g;   #Replace capital letters for LaTeX and abbreviate;
    $_ =~ s/Centre for Economic Policy Research/{CEPR}/g; #Replace for convenience, and mark capital letters for LaTeX and abbreviate;
    $_ =~ s/Keynes/{K}eynes/g;
    $_ =~ s/Ramsey/{R}amsey/g;
    $_ =~ s/Schumpeter/{S}chumpeter/g;

    # Replaces roman volume numbering. Should not replace page numbers since these are comma separated
    $_ =~ s/\s+II\./ 2./g;
    $_ =~ s/\s+III\./ 3./g;
    $_ =~ s/\s+IV\./ 4./g;
    $_ =~ s/\s+V\./ 5./g;
    $_ =~ s/\s+VI\./ 6./g;
    $_ =~ s/\s+VII\./ 7./g;
    $_ =~ s/\s+VIII\./ 8./g;
    $_ =~ s/\s+IX\./ 9./g;
    $_ =~ s/\s+X\./ 10./g;
    $_ =~ s/\s+XI\./ 11./g;
    $_ =~ s/\s+XII\./ 12./g;
    $_ =~ s/\s+ii\./ 2./g;
    $_ =~ s/\s+iii\./ 3\./g;
    $_ =~ s/\s+iv\./ 4./g;
    $_ =~ s/\s+v\./ 5./g;
    $_ =~ s/\s+vi\./ 6./g;
    $_ =~ s/\s+vii\./ 7./g;
    $_ =~ s/\s+viii\./ 8./g;
    $_ =~ s/\s+ix\./ 9./g;
    $_ =~ s/\s+x\./ 10./g;
    $_ =~ s/\s+xi\./ 11./g;
    $_ =~ s/\s+xii\./ 12./g;

    # Avoid confusion when entries contain "St. "
    $_ =~ s/\s+St\.\s+/ StXXX /g;  # will be reset later
    $_ =~ s/-St\.-/-StXXX-/g;  # will be reset later
       
      if(/TI:/){$count++; 
                $line=$_; $line =~ s/TI:(\s+)// ; $line =~ s/\n// ; $line =~ s/  / /g ; 
                $line =~ s/\[/``/g ; $line =~ s/\]/''/g ; # $line =~ s/\.\b//g ; 
                @TI[$count]= $line ; } 
      if(/AU:/){$line=$_; $line =~ s/AU:(\s+)// ; $line =~ s/\n// ; $line =~ s/-/ /g; 
                $line =~ s/;/ and/g; 
                $line =~ s/\,\s+et\s+al\./ etXal/; $line =~ s/\s+et\s+al\./ etXal/;
                $line =~ s/\,\s+et\s+al/ etXal/;   $line =~ s/\s+et\s+al/ etXal/;
                @AU[$count]= $line ; }
      if(/ED:/){$line=$_; $line =~ s/ED:(\s+)// ; $line =~ s/\n// ; $line =~ s/-/ /g; 
                $line =~ s/;/ and/g; 
                $line =~ s/\,\s+et\s+al\./ etXal/; $line =~ s/\s+et\s+al\./ etXal/;
                $line =~ s/\,\s+et\s+al/ etXal/;   $line =~ s/\s+et\s+al/ etXal/;
                $line =~ s/\, eds\.//;  $line =~ s/\, ed\.//;  $line =~ s/\, eds//;  $line =~ s/\, ed//;
                @ED[$count]= $line ; }
    # if(/AF:/){$line=$_; $line =~ s/AF:(\s+)// ; $line =~ s/\n// ; @AF[$count]= $line ; }
      if(/SO:/){$line=$_; $line =~ s/SO:(\s+)// ; $line =~ s/\n// ; @SO[$count]= $line ; }
      if(/PB:/){$line=$_; $line =~ s/PB:(\s+)// ; $line =~ s/\n// ; @PB[$count]= $line ; }
      if(/FTXT:/){$line=$_; $line =~ s/FTXT:(\s+)// ; $line =~ s/\n// ; @FT[$count]= $line ; }
      if(/DT:/){$line=$_; $line =~ s/DT:(\s+)// ; $line =~ s/\n// ; @DT[$count]= $line ; $noprbdt++ ; }
      if(/PY:/){$line=$_; $line =~ s/PY:(\s+)// ; $line =~ s/\n// ; 
                $line =~ s/\[(\d+)\]//; if($1 =~ m/(\d+)/){ @RM[$count]= 'Previously published '.$1; };
                $line =~ s/\s+//g ; @PY[$count]= $line ; }
      if(/DE:/){$line=$_; $line =~ s/DE:(\s+)// ; $line =~ s/\n// ; $line =~ s/-/ /g; $line =~ s/;/ and/g; @DE[$count]= $line ; }
    # if(/GE:/){$line=$_; $line =~ s/GE:(\s+)// ; $line =~ s/\n// ; @GE[$count]= $line ; }
      if(/AB:/){$line=$_; $line =~ s/AB:(\s+)// ; $line =~ s/\n// ; $line =~ s/  / /g; @AB[$count]= $line ; }
    # if(/AI:/){$line=$_; $line =~ s/AI:(\s+)// ; $line =~ s/\n// ; @AI[$count]= $line ; }
    # if(/UD:/){$line=$_; $line =~ s/UD:(\s+)// ; $line =~ s/\n// ; @UD[$count]= $line ; }
      if(/AV:/){$line=$_; $line =~ s/AV:(\s+)// ; $line =~ s/\n// ; @AV[$count]= $line ; }
    # if(/PR:/){$line=$_; $line =~ s/PR:(\s+)// ; $line =~ s/\n// ; @PR[$count]= $line ; }
    # if(/CR:/){$line=$_; $line =~ s/CR:(\s+)// ; $line =~ s/\n// ; @CR[$count]= $line ; }
    # if(/IS:/){$line=$_; $line =~ s/IS:(\s+)// ; $line =~ s/\n// ; @IS[$count]= $line ; }
      if(/IB:/){$line=$_; $line =~ s/IB:(\s+)// ; $line =~ s/\.// ; $line =~ s/\n// ; @IB[$count]= $line ; }
    # if(/AN:/){$line=$_; $line =~ s/AN:(\s+)// ; $line =~ s/\n// ; @AN[$count]= $line ; }

} # End while loop for read-in

print OUT '% Please note: '. "\n" ;
print OUT '% * Author= or editor= names containing prefixes such as "de", "van", "von", "Jr.", and the like, may need to be adjusted manually using curly brackets.'. "\n" ;
print OUT '%   Please check all INCOLLECTION entries manually. Treatment maybe incomplete due to variation in AEA EconLit data.'. "\n" ;
print OUT '% * In particular, you may want to check for subtitles in INCOLLECTION entries. AEA EconLit does not allow to distinguish between series and subtitles.'. "\n" ;
print OUT '% * BibTeX does not allow both a volume and a number for a BOOK or an INCOLLECTION item. Choose option [-v] to select'. "\n" ;
print OUT '%   In addition, when books were published in a series, AEA EconLit may have erroneously interchanged numbers and volumes, please check.'. "\n" ;
print OUT '% * The keywords= entry only contains JEL numbers (if available). Different classification systems are ignored.'. "\n" ;
print OUT '% * Book-Reviews are suppressed'. "\n" ;
#print OUT '%Please pay close attention to the following In-Collection entries: (some AEA EconLit data may have been too different from common patterns to be parsed properly)'. "\n" ; 
print OUT "\n" ;

for(my $i=1;$i<=$count;$i++){  # Data preparation loops
                               # print OUT "$i\:". @DT[$i] ."  "; # records entries on which some evaluation was performed
    if(@PB[$i] =~ s/\,*\s*([\d+\w+]+st)\s+[eE]dition\.*\s*// || @PB[$i] =~ s/\,*\s*([\d+\w+]+nd)\s+[eE]dition\.*\s*// || @PB[$i] =~ s/\,*\s*([\d+\w+]+rd)\s+[eE]dition\.*\s*// || @PB[$i] =~ s/\,*\s*([\d+\w+]+th)\s+[eE]dition\.*\s*//){
              # print OUT "Got an item with recorded edition number. $i\:". @PB[$i] ." \n";
              @EN[$i]= $1;
              @EN[$i]=~ tr/A-Z/a-z/;
              }
    if(@SO[$i] =~ s/\,*\s*([\d+\w+]+st)\s+[eE]dition\.*\s*// || @SO[$i] =~ s/\,*\s*([\d+\w+]+nd)\s+[eE]dition\.*\s*// || @SO[$i] =~ s/\,*\s*([\d+\w+]+rd)\s+[eE]dition\.*\s*// || @SO[$i] =~ s/\,*\s*([\d+\w+]+th)\s+[eE]dition\.*\s*//){
              # print OUT "Got an item with recorded edition number. $i\:". @SO[$i] ." \n";
              @EN[$i]= $1;
              @EN[$i]=~ tr/A-Z/a-z/;
              }
    if(@DT[$i] =~ m/Journal Article/){
              # print OUT "Got an ARTICLE item. $i\:". @SO[$i] ." \n";
              $_ = @SO[$i];  # structure like "American Economic Review, 74(3), June 1984, pp. 433-44"
                             # or             "Economic Policy: A European Forum, 0(20), April 1995, pp. 53-97"
                             # or             "Carnegie-Rochester Conference Series on Public Policy, 41(0), December 1994, pp. 57-65"
                             # or             "Brookings Papers on Economic Activity, 0(0), Special Issue 1990, pp. 287-321"
                             # or             "Federal Reserve Bank of Richmond Economic Review, 73(2), March-April 1987, pp. 12-30"
                             # or             "Economic Journal, 111(475), November 2001, pp. F654-99"
                             # or             "Journal of Political Economy, 98(5), Part 2 October 1990, pp. S71-102"
                             # or             "Weltwirtschaftliches Archiv/Review of World Economics, 135(4), 1999, pp. 675-91"

              $_ =~ s/Journal of Law, Economics, and Organization/Journal of Law Economics and Organization/g;
              $_ =~ s/Journal of Money, Credit, and Banking/Journal of Money Credit and Banking/g; 

              $_ =~ s/^([\w\-\/\{\}\:\(\)\'\s]+)\,\s//;
              $jo = $1; $jo =~ s/\{//g; $jo =~ s/\}//g; 
              @JO[$i]= $jo;    
              $_ =~ s/^([0-9]+)\(([0-9\-]+)\)\,\s?([0-9a-zA-Z\s\-]*)\s[0-9]+\,\spp\.\s([A-Z0-9\-]+)//; 
              @VO[$i]= $1; 
              @NO[$i]= $2;
              @MO[$i]= $3;
              @PG[$i]= $4; 
              @RM[$i] = $_;  # Remainder of SO: string go to note={}
              if(@FT[$i] =~ m/GOTO\(/){ $_ = @FT[$i]; /GOTO\(\d+\,([\w\S\.\:\/\~\&\?+]+)\)/; @SO[$i] = $1; }
                 else                 { $_ = @AV[$i]; @SO[$i] = @AV[$i]; } 
              $kw = @DE[$i]; $kw =~ s/\(\d+\) //g; $kw =~ s/ \(\d+\)//g; @KW[$i] = $kw; 
              if(@JO[$i] !~ m/\w+/ || @VO[$i] !~ m/\w+/ || @PG[$i] !~ m/\d+/ || (@PG[$i]==@NO[$i] && @MO[$i]==@VO[$i]) || @MO[$i] =~ m/0/ || @MO[$i] =~ m/5-9/ ){
                $probljo++;
                if($probljo==1){ print OUT '%ARTICLE entries may be incomplete! Please check all entries on the list. (This problem can be due to non-standard entries in AEA EconLit.)'. "\n" ; warn 'There were @ARTICLE entries with problems. See head of output file for details.'. "\n". 'Error occured';}
                print OUT "\%$i\: @AU[$i], @TI[$i] \n";
                }
    }
    elsif(@DT[$i] =~ m/Working Paper/){
              # print OUT "Got an ARTICLE (Working-Paper) item. $i\:". @SO[$i] ." \n";
              $_ = @SO[$i];  # structure is like "SO: National Bureau of Economic Research, Inc, NBER Working Papers: 12927, 2007."
                             # or                "SO: National Bureau of Economic Research, Inc, NBER Working Papers: 6811, 1998, pp."
              $_ =~ s/\,\s+pp\.\s+[0-9]+\s+\w+//g; $_ =~ s/\,\s+pp\s+[0-9]+\s+\w+//g; $_ =~ s/\,\s+pp\.//g; $_ =~ s/\,\s+pp//g; # remove "pp." or "pp" from line
              $_ =~ s/[\.\,]+\s@PY[$i]\.//g; $_ =~ s/[\.\,]+\s@PY[$i]\,//g; $_ =~ s/[\.\,]+\s@PY[$i]//g; $_ =~ s/\,\s\.//g; $_ =~ s/\,\s\,//g;  # remove year from line
              /([a-zA-Z\,\s\{\}\.]+)\:\s+([a-zA-Z0-9\/\-]+)/;
              @JO[$i]= $1; 
              @VO[$i]= $2;
              if(@JO[$i] !~ m/\w+/){
                $_ = @SO[$i];  # structure is like "SO: National Bureau of Economic Research Working Paper: 7440 December 1999; 13"
                $_ =~ s/\,\s+pp\.\s+[0-9]+\s+\w+//g; $_ =~ s/\,\s+pp\s+[0-9]+\s+\w+//g; $_ =~ s/\,\s+pp\.//g; $_ =~ s/\,\s+pp//g; # remove "pp." or "pp" from line
                /([a-zA-Z\,\s\{\}\.]+)\:\s+([a-zA-Z0-9\/\-]+)\s+(\w+)\s+(\d+)/;
                @JO[$i]= $1; 
                @VO[$i]= $2;
                @MO[$i]= $3;
                if($4 =~ m/\w+/){ @PY[$i]= $4; }
                }
              if(@JO[$i] !~ m/\w+/){
                $_ = @SO[$i];  # structure is like "SO: Centre for the Study of African Economies Working Paper: WPS/00/07 2000; 30"
                $_ =~ s/\,\s+pp\.\s+[0-9]+\s+\w+//g; $_ =~ s/\,\s+pp\s+[0-9]+\s+\w+//g; $_ =~ s/\,\s+pp\.//g; $_ =~ s/\,\s+pp//g; # remove "pp." or "pp" from line
                /([a-zA-Z\,\s\{\}\.]+)\:\s+([a-zA-Z0-9\/\-]+)\s+(\d+)/;
                @JO[$i]= $1;
                @VO[$i]= $2;
                if($3 =~ m/\w+/){ @PY[$i]= $3; }
                }
              if(@JO[$i] !~ m/\w+/){
                $_ = @SO[$i];  # structure is like  "SO: University of Oxford, Department of Economics, Economics Series Working Papers, 1998, pp. 34 pages"
                $_ =~ s/\,\s+pp\.\s+[0-9]+\s+\w+//g; $_ =~ s/\,\s+pp\s+[0-9]+\s+\w+//g; $_ =~ s/\,\s+pp\.//g; $_ =~ s/\,\s+pp//g; # remove "pp." or "pp" from line
                $_ =~ s/[\.\,]+\s@PY[$i]\.//g; $_ =~ s/[\.\,]+\s@PY[$i]\,//g; $_ =~ s/[\.\,]+\s@PY[$i]//g; $_ =~ s/\,\s\.//g; $_ =~ s/\,\s\,//g;  # remove year from line
                $_ =~ s/\,//g;  # remove commas
                $_ =~ s/(\w+)\s\1/$1 /g;  # remove duplicate words
                /([a-zA-Z\s\{\}]+)/;
                @JO[$i]= $1;
                }
              if(@JO[$i] !~ m/\w+/){
                $_ = @SO[$i];  # structure was NOT like any above
                               # try alternatives; one is given below, further alternatives may be needed
                $_ =~ s/\,\s+pp\.\s+[0-9]+\s+\w+//g; $_ =~ s/\,\s+pp\s+[0-9]+\s+\w+//g; $_ =~ s/\,\s+pp\.//g; $_ =~ s/\,\s+pp//g; # remove "pp." or "pp" from line
                /([a-zA-Z\,\s\{\}\.]+)(\.*)\s+([a-zA-Z0-9\/\-]+)\s+(\w+)\s+(\d+)/;
                @JO[$i]= $1;
                @VO[$i]= $2;
                @MO[$i]= $3;    
                if($4 =~ m/\w+/){ @PY[$i]= $4; }
                }
              # @RM[$i] = $_ . @PY[$i];  # Use this line to display remainder of SO:
              $jo= @JO[$i]; $jo =~ s/\,//g; $jo =~ s/ Series//g; $jo =~ s/ Papers/ Paper/g; $jo =~ s/ papers/ paper/g; @JO[$i]=$jo;
              $vo= @VO[$i]; $vo =~ s/[a-zA-Z]+\///g; $vo =~ s/[a-zA-Z]+\-//g; @VO[$i] = $vo; $vo = "";
              if(@FT[$i] =~ m/GOTO\(/){ $_ = @FT[$i]; /GOTO\(\d+\,([\w\S\.\:\/\~\&\?+]+)\)/; @SO[$i] = $1; }
                 else                 { $_ = @AV[$i]; @SO[$i] = @AV[$i]; } 
              $sr = @SO[$i]; $sr =~ s/Copies available from\:\s//g; $sr =~ s/\{//g; $sr =~ s/\}//g; @SO[$i] = $sr ;
              $kw = @DE[$i]; $kw =~ s/\(\d+\) //g; $kw =~ s/ \(\d+\)//g; @KW[$i] = $kw; 
              if($option !~ m/\-t/ && @JO[$i] !~ m/paper/ && @JO[$i] !~ m/Paper/ ){
                    @JO[$i] = @JO[$i] . ' Working Paper'}
              if(@JO[$i] !~ m/\w+/ || @VO[$i] !~ m/\w+/){
                $problwp++;
                if($problwp==1){ print OUT '%ARTICLE (Working-Paper) entries may be incomplete! Please check all entries on the list. (This problem can be due to non-standard entries in AEA EconLit.)'. "\n" ; warn 'There were @ARTICLE (Working-Paper) entries with problems. See head of output file for details.'. "\n". 'Error occured';}
                print OUT "\%$i\: @AU[$i], @TI[$i] \n";
                }
    }
    elsif(@DT[$i] =~ m/Book/ && @DT[$i] !~ m/Book Review/){
              # print OUT "Got a BOOK. $i\:". @PB[$i] ."  ";
              $_ = @PB[$i];  
              $_ =~ s/\.\s+[pP]reviously\s+[pP]ublished\:\s+\d+\.$//; # . Previously published: 1999.
              $_ =~ s/[\.\,]+\s@PY[$i]\.//g; $_ =~ s/[\.\,]+\s@PY[$i]\,//g; $_ =~ s/[\.\,]+\s@PY[$i]//g; $_ =~ s/\,\s\.//g; $_ =~ s/\,\s\,//g;  # remove year from line
              $_ =~ s/\s+/ /g;   # Remove double spaces
              $_ =~ s/\.\./\./g; # Remove double dots
              $_ =~ s/[\,\.\;]*\spp\.\s([mclxvi\d\,\s\-]+)//;
              $_ =~ s/[vV]ol[umes]*\.*\s+([\d+\-]+)/VOL $1/g;
              $_ =~ s/[nN]o[s]*\.*\s+([\d+\-]+)/NUM $1/g;
              $_ =~ s/[nN]um[bers]*\.*\s+([\d+\-]+)/NUM $1/g;
              $_ =~ s/VOL\s+(\d+)\s+([a-zA-Z0-9\,\s\{\}\-\'\`]+)/VOL $1. $2/;
              $_ =~ s/NUM\s+(\d+)\s+([a-zA-Z0-9\,\s\{\}\-\'\`]+)/NUM $1. $2/;
              $_ =~ s/\;\s+([mclxvi\d\-]+)$//;
              $_ =~ s/\s+([A-Z])\.\s+/ $1 /;  $_ =~ s/\s+([A-Z])\.\s+/ $1 /;  $_ =~ s/\s+([A-Z])\.\s+/ $1 /; # remove dots from capital letter middle names
              $_ =~ s/\,\s+@PY[$i]$//; $_ =~ s/\;\s+@PY[$i]$//; $_ =~ s/\s+@PY[$i]$//; $_ =~ s/\.\s+@PY[$i]$//; 
              $_ =~ s/[tT]ranslat[ion]*\s+[a-zA-Z0-9\,\s\{\}\-\'\`]*\.\s*//;
              $_ =~ s/[fF]oreword\s+by\s+[a-zA-Z0-9\,\s\{\}\-\'\`]*\.\s*//;
              # Chop off atypically positioned festschrift type entries. Structure is like "PB: Preferences, uncertainty, and optimality: Essays in honor of Leonid Hurwicz. Westview Press 1990"
              if ($_ =~ s/\s+([a-zA-Z0-9\,\s\{\}\-\'\`]+\:\s+[a-zA-Z0-9\,\s\{\}\-\'\`]*[hH]onou*r[ing]*\s+[a-zA-Z0-9\,\s\{\}\-\'\`]+)\.// || 
                  $_ =~ s/\s+([a-zA-Z0-9\,\s\{\}\-\'\`]+\:\s+[a-zA-Z0-9\,\s\{\}\-\'\`]*[fF]estschrift\s+[a-zA-Z0-9\,\s\{\}\-\'\`]+)\.//){
                  @TI[$i]= $1;
                  }
              $_ =~ s/\;\s+in\s+association\s+with\s+[a-zA-Z0-9\,\s\{\}\-\'\`]//; # string like "; in association with International Economic Association" chopped off
              # @RM[$i]= $_;   # Use this line to display SO:

              # Turn distributor into source
              $_ =~ s/\;\s+[dD](istrib[\.uted]*\s+[a-zA-Z0-9\,\s\{\}\-\'\`]*by\s+[a-zA-Z0-9\,\s\{\}\-\'\`]+)//; $so = $1; if($1 =~ m/istrib/g){ @SO[$i] = 'D'.$so; }

              # @RM[$i]= $_;   # Use this line to display remainder of SO:
              # Assign Address AD and Publisher PB, as well as series SE if available
              # Treat complex structures with series or handbook entries
              # Structure is like "Handbooks in Economics, NUM 10. Amsterdam; Oxford and Tokyo: {N}orth-{H}olland"
              if($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[hH]andbook[s]?\s*[a-zA-Z0-9\s\{\}\(\)\-]*)\,\s+[NUM]*[VOL]*\s+([\d]+)\.\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, vol. Address: Publisher
                  @SE[$i]= $1;
                  @NO[$i]= $2;
                  @AD[$i]= $3;
                  @PB[$i]= $4;
                  }
              # Structure is like "Elgar Reference Collection. Globalization of the World Economy series, VOL 19. Cheltenham, {UK} and {N}orthampton, {MA}: Elgar"
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[sS]eries\s*[a-zA-Z0-9\s\{\}\(\)\-]*)\,\s+[NUM]*[VOL]*\s+([\d]+)\.\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, vol. Address: Publisher
                  @SE[$i]= $1;
                  @NO[$i]= $2;
                  @AD[$i]= $3;
                  @PB[$i]= $4;
                  }
              # Structure is like "{NBER} Studies in Business Cycles, VOL 30. Chicago and London: University of Chicago Press"
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[sS]tudies\s*[a-zA-Z0-9\s\{\}\(\)\-]*)\,\s+[NUM]*[VOL]*\s+([\d]+)\.\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, vol. Address: Publisher
                  @SE[$i]= $1;
                  @NO[$i]= $2;
                  @AD[$i]= $3;
                  @PB[$i]= $4;
                  }
              # Structure is like "International Library of Critical Writings in Economics, VOL 16. Aldershot, {UK}: Elgar"
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[wW]riting[s]?\s*[a-zA-Z0-9\s\{\}\(\)\-]*)\,\s+[NUM]*[VOL]*\s+([\d]+)\.\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, vol. Address: Publisher
                  @SE[$i]= $1;
                  @NO[$i]= $2;
                  @AD[$i]= $3;
                  @PB[$i]= $4;
                  }
              # Structure is like "Elgar Reference Collection. Economic Approaches to Law, VOL 16. Cheltenham, {UK} and {N}orthampton, {MA}: Elgar"
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[aA]pproach[es]?\s*[a-zA-Z0-9\s\{\}\(\)\-]*)\,\s+[NUM]*[VOL]*\s+([\d]+)\.\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, vol. Address: Publisher
                  @SE[$i]= $1;
                  @NO[$i]= $2;
                  @AD[$i]= $3;
                  @PB[$i]= $4;
                  }
              # Structure is like "{LSE} Handbooks in Economics. Cambridge, {M}ass.: {MIT} Press"
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[hH]andbook[s]?\s+in[\s\d\w]*\s+[eE]conomics)\.*\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, NO vol. Address: Publisher
                  @SE[$i]= $1;
                  @AD[$i]= $2;
                  @PB[$i]= $3;
                  }
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[hH]andbook[s]?\s+in[\s\d\w]*\s+[hH]istory)\.*\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, NO vol. Address: Publisher
                  @SE[$i]= $1;
                  @AD[$i]= $2;
                  @PB[$i]= $3;
                  }
              # Structure is like "Studies in Monetary and Financial History Cambridge; New York and Melbourne: Cambridge University Press"
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[sS]tudies\s+in[\s\d\w]*\s+[eE]conomics)\.*\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, NO vol. Address: Publisher
                  @SE[$i]= $1;
                  @AD[$i]= $2;
                  @PB[$i]= $3;
                  }
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[sS]tudies\s+in[\s\d\w]*\s+[hH]istory)\.*\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, NO vol. Address: Publisher
                  @SE[$i]= $1;
                  @AD[$i]= $2;
                  @PB[$i]= $3;
                  }
              # Structure is like "{NBER} Conference Report series. Chicago and London: University of Chicago Press"
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[sS]eries)\.?\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, NO vol. Address: Publisher
                  @SE[$i]= $1;
                  @AD[$i]= $2;
                  @PB[$i]= $3;
                  }
              # Structure is like "{MIT} Press Readings in Economics Cambridge, {MA} and London: {MIT} Press"
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[rR]eading[s]?\s+in[\s\d\w]*\s+[eE]conomics)\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, NO vol. Address: Publisher
                  @SE[$i]= $1;
                  @AD[$i]= $2;
                  @PB[$i]= $3;
                  }
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[rR]eading[s]?\s+in[\s\d\w]*\s+[hH]istory)\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, NO vol. Address: Publisher
                  @SE[$i]= $1;
                  @AD[$i]= $2;
                  @PB[$i]= $3;
                  }
              # Treat simple structure with no series entry
              # Structure is like "PB: Cambridge, Mass.: Harvard University Press"
              elsif($_ =~ s/([a-zA-Z0-9\,\.\;\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Address: Publisher
                  @AD[$i]= $1;
                  @PB[$i]= $2;
                  }
              # @RM[$i]= $_;   # Use this line to display remainder of SO:

              # "With" additional author
              # @RM[$i]= @AD[$i];   # Use this line to display AD:
              # Structure is like "With Edward C Prescott Cambridge, {MA} and London: Harvard University Press"
              if(@AD[$i] =~ s/([wW]ith\s[a-zA-Z]+\s[A-Z]+\s[a-zA-Z]+)\.?\,?\s//g){ @RM[$i]= $1; @RM[$i] =~ s/\s+([A-Z])\s+/ $1. /g; @RM[$i] =~ s/^([A-Z])\s+/$1. /; }
              elsif(@AD[$i] =~ s/([wW]ith\s[a-zA-Z]+\s[a-zA-Z]+)\.?\,?\s//g){ @RM[$i]= $1; }

              # Ancillary treatments
              if(@TI[$i] =~ s/[\,\;\.]*\s*Volume[s]*\s+([\d\-]+)//){
                        @VOTMP[$i] = $1;
                        if(@VO[$i] =~ m/\d+/ && @NO[$i] !~ m/d+/){@NO[$i]= @VOTMP[$i]; }
                        else {@VO[$i]= @NO[$i]; @NO[$i]= @VOTMP[$i]; }
                        # print OUT 'Got a volume entry (from title) at '. "$i \n";
                        }
              $se = @SE[$i]; $se =~ s/Volume\s+//g; $se =~ s/volume\s+//g; $se =~ s/Vol\.\s+//g; $se =~ s/vol\.\s+//g; 
                             $se =~ s/Number\s+//g; $se =~ s/number\s+//g; $se =~ s/No\.\s+//g; $se =~ s/no\.\s+//g; 
                             if($se !~ m/([a-zA-Z]+)/){@SE[$i] = ""; }  # remove series if only Volume, etc. contained

              if(@TI[$i] !~ m/\w+/ && @SE[$i] =~ m/\w+/ && @SE[$i] !~ m/[sS]eries/){
                # If booktitle still empty, make series entry the booktitle
                  @TI[$i]= @SE[$i];
                  @SE[$i]= "";
                }
              @SE[$i] =~ s/\s+([B-Z])\s+/ $1. /g; @SE[$i] =~ s/^([B-Z])\s+/$1. /; # add back dots to capital letter first or middle names
              @TI[$i] =~ s/\s+([B-Z])\s+/ $1. /g; @TI[$i] =~ s/^([B-Z])\s+/$1. /; # add back dots to capital letter first or middle names

              @AD[$i] =~ s/\; /, /g;
              @PB[$i] =~ s/\s+([A-Z])\s+/ $1. /g; @PB[$i] =~ s/^([A-Z])\s+/$1. /; # add back dots to capital letter first or middle names
              @SE[$i] =~ s/\.\. /. /g; @SE[$i] =~ s/\s\s/ /g; @SE[$i] =~ s/[\.]\s*$//g;
              if(@SE[$i] !~ m/\w/){ @SE[$i] = $_ }                          
              if(@SE[$i] =~ m/Paperback/){@SE[$i]=""}
              if(@SE[$i] =~ m/paperback/){@SE[$i]=""}
              if(@SE[$i] =~ m/Revised edition/){@SE[$i]=""}
              if(@SE[$i] =~ m/revised edition/){@SE[$i]=""}
              # @RM[$i]= @SE[$i];   # Use this line to display @SE[$i] before further treatment

              $kw = @DE[$i]; $kw =~ s/\(\d+\) //g; $kw =~ s/ \(\d+\)//g; @KW[$i] = $kw; 
              if(@PB[$i] !~ m/\w+/ || @PB[$i] =~ m/;/ || @AD[$i] !~ m/\w+/ || (@VO[$i] =~ m/\d+/ && @SE[$i] !~ m/\w+/) || (@NO[$i] =~ m/\d+/ && @SE[$i] !~ m/\w+/)){
                $problbk++;
                if($problbk==1){ print OUT '%BOOK entries may be incomplete! Please check all entries on the list. (This problem can be due to missing series information or to non-standard entries in WebSpirs.)'. "\n" ; warn 'There were @BOOK entries with problems. See head of output file for details.'. "\n". 'Error occured';}
                print OUT "\%$i\: @AU[$i], @TI[$i] \n";
                }
    }
    elsif(@DT[$i] =~ m/Collective Volume Article/){
              # print OUT "Got an INCOLLECTION item. $i\:". @SO[$i] ." \n";

              # WORK ON SO: (including PG BT VO NO)
              $_ = @SO[$i];  
              $_ =~ s/\.\s+[pP]reviously\s+[pP]ublished\:\s+\d+\.$//; # . Previously published: 1999.
              $_ =~ s/[\.\,]+\s@PY[$i]\.//g; $_ =~ s/[\.\,]+\s@PY[$i]\,//g; $_ =~ s/[\.\,]+\s@PY[$i]//g; $_ =~ s/\,\s\.//g; $_ =~ s/\,\s\,//g;  # remove year from line
              $_ =~ s/\s+/ /g;   # Remove double spaces
              $_ =~ s/\.\./\./g; # Remove double dots
              $_ =~ s/[\,\.\;]*\spp\.\s([0-9\-]+)//;
              @PG[$i]= $1;
              $_ =~ s/[vV]ol[umes]*\.*\s+([\d+\-]+)/VOL $1/g;
              $_ =~ s/[nN]o[s]*\.*\s+([\d+\-]+)/NUM $1/g;
              $_ =~ s/[nN]um[bers]*\.*\s+([\d+\-]+)/NUM $1/g;
              $_ =~ s/VOL\s+(\d+)\s+([a-zA-Z0-9\,\s\{\}\-\'\`]+)/VOL $1. $2/;
              $_ =~ s/NUM\s+(\d+)\s+([a-zA-Z0-9\,\s\{\}\-\'\`]+)/NUM $1. $2/;
              $_ =~ s/\;\s+([mclxvi\d\-]+)$//;
              $_ =~ s/\s+([A-Z])\.\s+/ $1 /;  $_ =~ s/\s+([A-Z])\.\s+/ $1 /;  $_ =~ s/\s+([A-Z])\.\s+/ $1 /; # remove dots from capital letter middle names
              $_ =~ s/\,\s+@PY[$i]$//; $_ =~ s/\;\s+@PY[$i]$//; $_ =~ s/\s+@PY[$i]$//; $_ =~ s/\.\s+@PY[$i]$//; 
              $_ =~ s/\;\s+[dD]istrib[\.uted]*\s+[a-zA-Z0-9\,\s\{\}\-\'\`]*by\s+([a-zA-Z0-9\,\s\{\}\-\'\`]+)//; @SO[$i] = $1;  # turn distributor into source
              $_ =~ s/[tT]ranslat[ion]*\s+[a-zA-Z0-9\,\s\{\}\-\'\`]*\.\s*//;
              $_ =~ s/[fF]oreword\s+by\s+[a-zA-Z0-9\,\s\{\}\-\'\`]*\.\s*//;
              # Chop off atypically positioned festschrift type entries. Structure is like "SO: Preferences, uncertainty, and optimality:  Essays in honor  of Leonid Hurwicz.  Westview Press 1990"
              if ($_ =~ s/\s+([a-zA-Z0-9\,\s\{\}\-\'\`]+\:\s+[a-zA-Z0-9\,\s\{\}\-\'\`]*[hH]onou*r[ing]*\s+[a-zA-Z0-9\,\s\{\}\-\'\`]+)\.// || 
                  $_ =~ s/\s+([a-zA-Z0-9\,\s\{\}\-\'\`]+\:\s+[a-zA-Z0-9\,\s\{\}\-\'\`]*[fF]estschrift\s+[a-zA-Z0-9\,\s\{\}\-\'\`]+)\.//){
                  @BT[$i]= $1;
                  }
              $_ =~ s/\;\s+in\s+association\s+with\s+[a-zA-Z0-9\,\s\{\}\-\'\`]//; # string like "; in association with International Economic Association" chopped off
              # @RM[$i]= $_;   # Use this line to display SO:

              # Assign volume VO (number NO) and booktitle BT (move potential remainder into series SE)
              # Structure is like "SO: Exchange rate economics. Volume 1, 1992, pp. 61-103" (turned into "Exchange rate economics. VOL 1")
              if($_ =~ s/([a-zA-Z0-9\:\s\{\}\(\)\`\'\-]+)\.\s[NUM]*[VOL]*\.*\s+([\d]+)\,*([a-zA-Z0-9\,\:\?\s\{\}\(\)\-\'\`]*)//){ # Book title. Volume
                  @BT[$i]= $1;
                  @VO[$i]= $2;
                  @SE[$i]= $3;
                  }
              elsif($_ =~ s/([a-zA-Z0-9\:\s\{\}\(\)\`\'\-]+)//){ # Book title only
                  @BT[$i]= $1;
                  }
              # @RM[$i]= $_;   # Use this line to display remainder of SO:

              # WORK ON PB: (including AD PB SE)
              $_ = @PB[$i];  
              $_ =~ s/\s+/ /g;   # Remove double spaces
              $_ =~ s/\.\./\./g; # Remove double dots
              $_ =~ s/[vV]ol[umes]*\.*\s+([\d+\-]+)/VOL $1/g;
              $_ =~ s/[nN]o[s]*\.*\s+([\d+\-]+)/NUM $1/g;
              $_ =~ s/[nN]um[bers]*\.*\s+([\d+\-]+)/NUM $1/g;
              $_ =~ s/VOL\s+(\d+)\s+([a-zA-Z0-9\,\s\{\}\-\'\`]+)/VOL $1. $2/;
              $_ =~ s/NUM\s+(\d+)\s+([a-zA-Z0-9\,\s\{\}\-\'\`]+)/NUM $1. $2/;

              # Turn distributor into source
              $_ =~ s/\;\s+[dD](istrib[\.uted]*\s+[a-zA-Z0-9\,\s\{\}\-\'\`]*by\s+[a-zA-Z0-9\,\s\{\}\-\'\`]+)//; $so = $1; if($1 =~ m/istrib/g){ @SO[$i] = 'D'.$so; }

              # @RM[$i]= $_;   # Use this line to display remainder of SO:
              # Assign Address AD and Publisher PB, as well as series SE if available
              # Treat complex structures with series or handbook entries
              # Structure is like "Handbooks in Economics, NUM 10. Amsterdam; Oxford and Tokyo: {N}orth-{H}olland"
              if($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[hH]andbook[s]?\s*[a-zA-Z0-9\s\{\}\(\)\-]*)\,\s+[NUM]*[VOL]*\s+([\d]+)\.\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, vol. Address: Publisher
                  @SE[$i]= $1;
                  @NO[$i]= $2;
                  @AD[$i]= $3;
                  @PB[$i]= $4;
                  }
              # Structure is like "Elgar Reference Collection. Globalization of the World Economy series, VOL 19. Cheltenham, {UK} and {N}orthampton, {MA}: Elgar"
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[sS]eries\s*[a-zA-Z0-9\s\{\}\(\)\-]*)\,\s+[NUM]*[VOL]*\s+([\d]+)\.\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, vol. Address: Publisher
                  @SE[$i]= $1;
                  @NO[$i]= $2;
                  @AD[$i]= $3;
                  @PB[$i]= $4;
                  }
              # Structure is like "{NBER} Studies in Business Cycles, VOL 30. Chicago and London: University of Chicago Press"
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[sS]tudies\s*[a-zA-Z0-9\s\{\}\(\)\-]*)\,\s+[NUM]*[VOL]*\s+([\d]+)\.\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, vol. Address: Publisher
                  @SE[$i]= $1;
                  @NO[$i]= $2;
                  @AD[$i]= $3;
                  @PB[$i]= $4;
                  }
              # Structure is like "International Library of Critical Writings in Economics, VOL 16. Aldershot, {UK}: Elgar"
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[wW]riting[s]?\s*[a-zA-Z0-9\s\{\}\(\)\-]*)\,\s+[NUM]*[VOL]*\s+([\d]+)\.\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, vol. Address: Publisher
                  @SE[$i]= $1;
                  @NO[$i]= $2;
                  @AD[$i]= $3;
                  @PB[$i]= $4;
                  }
              # Structure is like "Elgar Reference Collection. Economic Approaches to Law, VOL 16. Cheltenham, {UK} and {N}orthampton, {MA}: Elgar"
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[aA]pproach[es]?\s*[a-zA-Z0-9\s\{\}\(\)\-]*)\,\s+[NUM]*[VOL]*\s+([\d]+)\.\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, vol. Address: Publisher
                  @SE[$i]= $1;
                  @NO[$i]= $2;
                  @AD[$i]= $3;
                  @PB[$i]= $4;
                  }
              # Structure is like "{LSE} Handbooks in Economics. Cambridge, {M}ass.: {MIT} Press"
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[hH]andbook[s]?\s+in[\s\d\w]*\s+[eE]conomics)\.*\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, vol. Address: Publisher
                  @SE[$i]= $1;
                  @AD[$i]= $2;
                  @PB[$i]= $3;
                  }
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[hH]andbook[s]?\s+in[\s\d\w]*\s+[hH]istory)\.*\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, vol. Address: Publisher
                  @SE[$i]= $1;
                  @AD[$i]= $2;
                  @PB[$i]= $3;
                  }
              # Structure is like "Studies in Monetary and Financial History Cambridge; New York and Melbourne: Cambridge University Press"
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[sS]tudies\s+in[\s\d\w]*\s+[eE]conomics)\.*\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, NO vol. Address: Publisher
                  @SE[$i]= $1;
                  @AD[$i]= $2;
                  @PB[$i]= $3;
                  }
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[sS]tudies\s+in[\s\d\w]*\s+[hH]istory)\.*\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, NO vol. Address: Publisher
                  @SE[$i]= $1;
                  @AD[$i]= $2;
                  @PB[$i]= $3;
                  }
              # Structure is like "{NBER} Conference Report series. Chicago and London: University of Chicago Press"
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[sS]eries)\.?\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, NO vol. Address: Publisher
                  @SE[$i]= $1;
                  @AD[$i]= $2;
                  @PB[$i]= $3;
                  }
              # Structure is like "{MIT} Press Readings in Economics Cambridge, {MA} and London: {MIT} Press"
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[rR]eading[s]?\s+in[\s\d\w]*\s+[eE]conomics)\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, NO vol. Address: Publisher
                  @SE[$i]= $1;
                  @AD[$i]= $2;
                  @PB[$i]= $3;
                  }
              elsif($_ =~ s/([a-zA-Z0-9\s\{\}\(\)\-]*\s*[rR]eading[s]?\s+in[\s\d\w]*\s+[hH]istory)\s+([a-zA-Z0-9\,\;\.\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Series, NO vol. Address: Publisher
                  @SE[$i]= $1;
                  @AD[$i]= $2;
                  @PB[$i]= $3;
                  }
              # Treat simple structure with no series entry
              # Structure is like "PB: Cambridge, Mass.: Harvard University Press"
              elsif($_ =~ s/([a-zA-Z0-9\,\.\;\s\{\}\(\)\`\'\-]+)\:\s*([a-zA-Z0-9\,\s\{\}\(\)\-\'\`]*)//){ # Address: Publisher
                  @AD[$i]= $1;
                  @PB[$i]= $2;
                  }
              # @RM[$i]= $_;   # Use this line to display remainder of SO:

              # Ancillary treatments
              if(@TI[$i] =~ s/[\,\;\.]*\s*Volume[s]*\s+([\d\-]+)//){
                        @VOTMP[$i] = $1;
                        if(@VO[$i] =~ m/\d+/ && @NO[$i] !~ m/d+/){@NO[$i]= @VOTMP[$i]; }
                        else {@VO[$i]= @NO[$i]; @NO[$i]= @VOTMP[$i]; }
                        # print OUT 'Got a volume entry (from title) at '. "$i \n";
                        }
              $se = @SE[$i]; $se =~ s/Volume\s+//g; $se =~ s/volume\s+//g; $se =~ s/Vol\.\s+//g; $se =~ s/vol\.\s+//g; 
                             $se =~ s/Number\s+//g; $se =~ s/number\s+//g; $se =~ s/No\.\s+//g; $se =~ s/no\.\s+//g; 
                             if($se !~ m/([a-zA-Z]+)/){@SE[$i] = ""; }  # remove series if only Volume, etc. contained

              if(@BT[$i] !~ m/\w+/ && @SE[$i] =~ m/\w+/ && @SE[$i] !~ m/[sS]eries/){
                # If booktitle still empty, make series entry the booktitle
                  @BT[$i]= @SE[$i];
                  @SE[$i]= "";
                }
              @SE[$i] =~ s/\s+([B-Z])\s+/ $1. /g; @SE[$i] =~ s/^([B-Z])\s+/$1. /; # add back dots to capital letter first or middle names
              @BT[$i] =~ s/\s+([B-Z])\s+/ $1. /g; @BT[$i] =~ s/^([B-Z])\s+/$1. /; # add back dots to capital letter first or middle names

              @AD[$i] =~ s/\; /, /g;
              @PB[$i] =~ s/\s+([A-Z])\s+/ $1. /g; @PB[$i] =~ s/^([A-Z])\s+/$1. /; # add back dots to capital letter first or middle names
              @SE[$i] =~ s/\.\. /. /g; @SE[$i] =~ s/\s\s/ /g; @SE[$i] =~ s/[\.]\s*$//g;
              if(@SE[$i] !~ m/\w/){ @SE[$i] = $_ }                          
              if(@SE[$i] =~ m/Paperback/){@SE[$i]=""}
              if(@SE[$i] =~ m/paperback/){@SE[$i]=""}
              if(@SE[$i] =~ m/Revised edition/){@SE[$i]=""}
              if(@SE[$i] =~ m/revised edition/){@SE[$i]=""}
              # @RM[$i]= @SE[$i];   # Use this line to display @SE[$i] before further treatment

              $kw = @DE[$i]; $kw =~ s/\(\d+\) //g; $kw =~ s/ \(\d+\)//g; @KW[$i] = $kw; 
              $tmp = @BT[$i]; $repcolon = ($tmp =~ s/\://g);
              if( $repcolon > 1 || @PG[$i] !~ m/[mclxvi\d]+/ || @PB[$i] =~ m/\;/ || @BT[$i] !~ m/\w+/ || @ED[$i] !~ m/\w+/ || @PB[$i] !~ m/\w+/ || @AD[$i] !~ m/\w+/ || (@VO[$i] =~ m/\d+/ && @SE[$i] !~ m/\w+/)){
                $problcl++;
                if($problcl==1){ print OUT '%INCOLLECTION entries may be incomplete! Please check all entries on the list. (This problem can be due to missing series information or to non-standard entries in AEA EconLit)'. "\n" ; warn 'There were @INCOLLECTION entries with problems. See head of output file for details.'. "\n". 'Error occured';}
                print OUT "\%$i\: @AU[$i], @TI[$i] \n";
                }
    }
    elsif(@DT[$i] =~ m/Dissertation/){
              # print OUT "Got a PHDTHESIS. $i\:". @SO[$i] ." \n";
              $_ = @PB[$i];    # structure is like "Ph.D.  University of Pennsylvania 1998"
              /Ph\.D\.\s+([\w+\s+\{\}]+)\s+\d+/;
              @SC[$i]=$1 ;
              $kw = @DE[$i]; $kw =~ s/\(\d+\) //g; $kw =~ s/ \(\d+\)//g; @KW[$i] = $kw; 
              if(@SC[$i] !~ m/\w+/){
                $problph++;
                if($problph==1){ print OUT '%PHDTHESIS entries may be incomplete! Please check all entries on the list. (This problem can be due to non-standard entries in AEA EconLit.)'. "\n" ; warn 'There were @PHDTHESIS entries with problems. See head of output file for details.'. "\n". 'Error occured';}
                print OUT "\%$i\: @AU[$i], @TI[$i] \n";
                }
    }
    
# Additional treatments

# Remove second English titles
@TI[$i] =~ s/\.\s+\([a-zA-Z0-9\,\.\s\{\}\-\'\`]+\.\)\s*$//;

# Reset volume and number entries to avoid BibTeX errors for Collective-Volume-Articles (loosing information here)
if((@DT[$i] =~ m/Collective Volume Article/ || @DT[$i] =~ m/Book/) && @NO[$i] =~ m/\d+/ && @SE[$i] !~ m/\w+/){@VO[$i] = @NO[$i]; @NO[$i] = "";}  
if((@DT[$i] =~ m/Collective Volume Article/ || @DT[$i] =~ m/Book/) && @NO[$i] =~ m/\d+/ && @VO[$i] =~ m/\d+/){
    if($option =~ m/\-v/){@NO[$i] = ""}
    else                 {@VO[$i] = ""}
    }  
if(@JO[$i] =~ m/Brookings Papers/){
    if(@NO[$i] =~ m/0/){@NO[$i] = ""}
    if(@VO[$i] =~ m/0/ && @NO[$i] =~ m/\d+/){@VO[$i] = @PY[$i]}
        else                                {@VO[$i] = ""}
    }

# Remove colliding interpunctuation, and other adjustments
@TI[$i] =~ s/\?\:/?/g; @TI[$i] =~ s/\?\./?/g;
@AB[$i] =~ s/\?\:/?/g; @AB[$i] =~ s/\?\./?/g;
@KW[$i] =~ s/\?\:/?/g; @KW[$i] =~ s/\?\./?/g;
@BT[$i] =~ s/\?\:/?/g; @BT[$i] =~ s/\?\./?/g;
@TI[$i] =~ s/\.\'/'/g; @BT[$i] =~ s/\.\'/'/g; @TI[$i] =~ s/\.\'\'/''/g; @BT[$i] =~ s/\.\'\'/''/g;  # Only at end of string
@TI[$i] =~ s/\,\'/'/g; @BT[$i] =~ s/\,\'/'/g; @TI[$i] =~ s/\,\'\'/''/g; @BT[$i] =~ s/\,\'\'/''/g;  # Only at end of string
@TI[$i] =~ s/\;\'/'/g; @BT[$i] =~ s/\;\'/'/g; @TI[$i] =~ s/\;\'\'/''/g; @BT[$i] =~ s/\;\'\'/''/g;  # Only at end of string
if($option =~ m/\-q/){@TI[$i] =~ s/\'\'/'/g; @TI[$i] =~ s/\`\`/`/g; @BT[$i] =~ s/\'\'/'/g; @BT[$i] =~ s/\`\`/`/g; }
@JO[$i] =~ s/\s+/ /g;
@SO[$i] =~ s/\s+/ /g;
@AB[$i] =~ s/\s+/ /g;
@KW[$i] =~ s/\s+/ /g;
@SE[$i] =~ s/^\s+//g;
 
# Insert punctuation in names
@AU[$i] =~ s/\s([A-Z])\s+/ $1. /g; @AU[$i] =~ s/\s([A-Z])\s+/ $1. /g;
@AU[$i] =~ s/\s([A-Z])\,\s+/ $1., /g;
@AU[$i] =~ s/\s([A-Z])$/ $1./; @AU[$i] =~ s/\s([A-Z])$/ $1./;
@ED[$i] =~ s/\s([A-Z])\s+/ $1. /g; @ED[$i] =~ s/\s([A-Z])\s+/ $1. /g;
@ED[$i] =~ s/\s([A-Z])\,\s+/ $1., /g;
@ED[$i] =~ s/\s([A-Z])$/ $1./; @ED[$i] =~ s/\s([A-Z])$/ $1./;
@AU[$i] =~ s/World Bank/{World Bank}/;
@ED[$i] =~ s/World Bank/{World Bank}/;
@AU[$i] =~ s/\{//g; @AU[$i] =~ s/\}//g;
@ED[$i] =~ s/\{//g; @ED[$i] =~ s/\}//g;
@AU[$i] =~ s/\,\s+([a-zA-Z]+)\s+([a-z]+)\s+and/\, $1\{-\}$2 and/g; @AU[$i] =~ s/\,\s+([a-zA-Z]+)\s+([a-z]+)$/\, $1\{-\}$2/g;
@ED[$i] =~ s/\,\s+([a-zA-Z]+)\s+([a-z]+)\s+and/\, $1\{-\}$2 and/g; @ED[$i] =~ s/\,\s+([a-zA-Z]+)\s+([a-z]+)$/\, $1\{-\}$2/g;

# Get "et al." citations ready for BibTeX styles with full first names
if($option =~ m/\-e/){
    @AU[$i] =~ s/\s+etXal/ {et~al.}/; 
    @ED[$i] =~ s/\s+etXal/ {et~al.}/; 
    }
else{
    @AU[$i] =~ s/\s+etXal/ et al./; 
    @ED[$i] =~ s/\s+etXal/ et al./; 
    }

# Reset "StXXX" to original "St." (confusion is over), and remove trailing blanks
@AU[$i] =~ s/StXXX/St./g;
@AU[$i] =~ s/[\s+\,]+$//;
@ED[$i] =~ s/StXXX/St./g;
@ED[$i] =~ s/[\s+\,]+$//;
@TI[$i] =~ s/StXXX/St./g;
@TI[$i] =~ s/[\s+\,\.\/]+$//;
@JO[$i] =~ s/StXXX/St./g;
@JO[$i] =~ s/[\s+\,\.\/]+$//;
@PY[$i] =~ s/StXXX/St./g;
@PY[$i] =~ s/[\s+\,\.\/]+$//;
@VO[$i] =~ s/StXXX/St./g;
@VO[$i] =~ s/[\s+\,\.\/]+$//;
@NO[$i] =~ s/StXXX/St./g;
@NO[$i] =~ s/[\s+\,\.\/]+$//;
@PG[$i] =~ s/StXXX/St./g;
@PG[$i] =~ s/[\s+\,\.\/]+$//;
@MO[$i] =~ s/StXXX/St./g;
@MO[$i] =~ s/[\s+\,\.\/]+$//;
@RM[$i] =~ s/StXXX/St./g;
if(@RM[$i] =~ m/\/$/){ @RM[$i] =~ s/\-/ /g; } # likely a foreign-language journal title
@RM[$i] =~ s/[\s+\,\.\/]+$//;
@AB[$i] =~ s/StXXX/St./g;
@AB[$i] =~ s/[\s+\,\.\/]+$//;
@KW[$i] =~ s/StXXX/St./g;
@KW[$i] =~ s/[\s+\,\.\/]+$//;
@SO[$i] =~ s/StXXX/St./g;
@SO[$i] =~ s/[\s+\,\.\/]+$//;
@BT[$i] =~ s/StXXX/St./g;
@BT[$i] =~ s/[\s+\,\.\/]+$//;
@PB[$i] =~ s/StXXX/St./g;
@PB[$i] =~ s/[\s+\,\.\/]+$//;
@SE[$i] =~ s/StXXX/St./g;
@SE[$i] =~ s/[\s+\,\.\/]+$//;
@TY[$i] =~ s/StXXX/St./g;
@TY[$i] =~ s/[\s+\,\.\/]+$//;
@CH[$i] =~ s/StXXX/St./g;
@CH[$i] =~ s/[\s+\,\.\/]+$//;
@AD[$i] =~ s/StXXX/St./g;
@AD[$i] =~ s/[\s+\,\.\/]+$//;
@EN[$i] =~ s/StXXX/St./g;
@EN[$i] =~ s/[\s+\,\.\/]+$//;
@IB[$i] =~ s/StXXX/St./g;
@IB[$i] =~ s/[\s+\,\.\/]+$//;
@SC[$i] =~ s/StXXX/St./g;
@SC[$i] =~ s/[\s+\,\.\/]+$//;

# Reset month to full string
$mt = @MO[$i]; $mt =~ s/\.//g; $mt =~ s/\//\-/g; 
if($option !~ m/\-m/){  # Do not perform month correction if option -m was set
    if($mt =~ m/Jan/ && $mt !~ m/Janu/){$mt =~ s/Jan/January/;}
    if($mt =~ m/Feb/ && $mt !~ m/Febr/){$mt =~ s/Feb/February/;}
    if($mt =~ m/Mar/ && $mt !~ m/Marc/){$mt =~ s/Mar/March/;}
    if($mt =~ m/Apr/ && $mt !~ m/Apri/){$mt =~ s/Apr/April/;}
    if($mt =~ m/Jun/ && $mt !~ m/June/){$mt =~ s/Jun/June/;}
    if($mt =~ m/Jul/ && $mt !~ m/July/){$mt =~ s/Jul/July/;}
    if($mt =~ m/Aug/ && $mt !~ m/Augu/){$mt =~ s/Aug/August/;}
    if($mt =~ m/Sept/ && $mt !~ m/Septe/){$mt =~ s/Sept/September/;}
    if($mt =~ m/Sep/ && $mt !~ m/Sept/){$mt =~ s/Sep/September/;}
    if($mt =~ m/Oct/ && $mt !~ m/Octo/){$mt =~ s/Oct/October/;}
    if($mt =~ m/Nov/ && $mt !~ m/Nove/){$mt =~ s/Nov/November/;}
    if($mt =~ m/Dec/ && $mt !~ m/Dece/){$mt =~ s/Dec/December/;}
    if($mt =~ m/Win/ && $mt !~ m/Wint/){$mt =~ s/Win/Winter/;}
    if($mt =~ m/Sum/ && $mt !~ m/Summ/){$mt =~ s/Sum/Summer/;}
    }
@MO[$i] = $mt;
$nm = @NO[$i]; $nm =~ s/\//\-/g; @NO[$i] = $nm;
# $vo = @VO[$i]; $vo =~ s/\//\\\//g; @VO[$i] = $vo;
if(@SO[$i] =~ m/@PB[$i]/){@SO[$i]="";}  # Avoids error of repeating publisher in source

# Reset abbreviations (confusion is over)
@TI[$i] =~ s/{UK}/{U.K.}/g;
@TI[$i] =~ s/{US}/{U.S.}/g;
@AD[$i] =~ s/{UK}/{U.K.}/g;
@AD[$i] =~ s/{US}/{U.S.}/g;
@AD[$i] =~ s/{CA}/{C}alif./g;
@AD[$i] =~ s/{CT}/{C}onn./g;
@AD[$i] =~ s/{DC}/{D.C.}/g;
@AD[$i] =~ s/{MA}/{M}ass./g;
@AD[$i] =~ s/{MD}/{M}d./g;
@AD[$i] =~ s/{MI}/{M}ich./g;
@AD[$i] =~ s/{NC}/{N.C.}/g;
@AD[$i] =~ s/{ND}/{N.D.}/g;
@AD[$i] =~ s/{NH}/{N.H.}/g;
@AD[$i] =~ s/{NJ}/{N.J.}/g;
@AD[$i] =~ s/{NM}/{N.M.}/g;
@AD[$i] =~ s/{NY}/{N.Y.}/g;
@AD[$i] =~ s/{PA}/{P}a./g;
@AD[$i] =~ s/{RI}/{R.I.}/g;
@AD[$i] =~ s/{SC}/{S.C.}/g;
@AD[$i] =~ s/{SD}/{S.D.}/g;
@AD[$i] =~ s/{VA}/{V}a./g;
@AD[$i] =~ s/{VT}/{V}t./g;
@AD[$i] =~ s/{WV}/{W.V.}/g;
@AD[$i] =~ s/{WI}/{W}isc./g;
@SO[$i] =~ s/{UK}/{U.K.}/g;
@SO[$i] =~ s/{US}/{U.S.}/g;
@SO[$i] =~ s/{CA}/{C}alif./g;
@SO[$i] =~ s/{CT}/{C}onn./g;
@SO[$i] =~ s/{DC}/{D.C.}/g;
@SO[$i] =~ s/{MA}/{M}ass./g;
@SO[$i] =~ s/{MD}/{M}d./g;
@SO[$i] =~ s/{MI}/{M}ich./g;
@SO[$i] =~ s/{NC}/{N.C.}/g;
@SO[$i] =~ s/{ND}/{N.D.}/g;
@SO[$i] =~ s/{NH}/{N.H.}/g;
@SO[$i] =~ s/{NJ}/{N.J.}/g;
@SO[$i] =~ s/{NM}/{N.M.}/g;
@SO[$i] =~ s/{NY}/{N.Y.}/g;
@SO[$i] =~ s/{PA}/{P}a./g;
@SO[$i] =~ s/{RI}/{R.I.}/g;
@SO[$i] =~ s/{SC}/{S.C.}/g;
@SO[$i] =~ s/{SD}/{S.D.}/g;
@SO[$i] =~ s/{VA}/{V}a./g;
@SO[$i] =~ s/{VT}/{V}t./g;
@SO[$i] =~ s/{WV}/{W.V.}/g;
@SO[$i] =~ s/{WI}/{W}isc./g;

} # End data preparation loops


# Create different BibTeX keys if option [-k] is set
if($option =~ m/\-k/){
    for(my $i=$count;$i>=1;$i--){
      if(@DT[$i] =~ m/Collective Volume Article/){$_ = @ED[$i];}
      else                                       {$_ = @AU[$i];}
      /\s+and\s+([\w+\-\`\']+)\,([\s+\w+\-\`\'\.]+)\s+and\s+([\w+\-\`\']+)\,/;
      $au3 = $3;
      /\s+and\s+([\w+\-\`\']+)\,/;
      $au2 = $1;
      /([\w+\-\`\']+)\,/;
      $au1 = $1;
      if($au3 =~ m/\w+/){
          if($au3 =~ m/(\w\w\w\w\w)/){$_ = $au3; /(\w\w\w\w\w)/; $au3 = $1;}
          if($au2 =~ m/(\w\w\w\w\w)/){$_ = $au2; /(\w\w\w\w\w)/; $au2 = $1;}
          if($au1 =~ m/(\w\w\w\w\w)/){$_ = $au1; /(\w\w\w\w\w)/; $au1 = $1;}
      }
      else {
          if($au2 =~ m/(\w\w\w\w\w\w)/){$_ = $au2; /(\w\w\w\w\w\w)/; $au2 = $1;}
          if($au1 =~ m/(\w\w\w\w\w\w)/){$_ = $au1; /(\w\w\w\w\w\w)/; $au1 = $1;}
      }
      $yr = @PY[$i]-1900; if($yr>99){$yr=$yr-100}; if($yr<10){$yr="0".$yr}; 
      @BIBKEY[$i]=$au1.$au2.$au3.$yr;
      if(@DT[$i] =~ m/Collective Volume Article/){@BIBKEY[$i]= @BIBKEY[$i].":"; @BIBREP[$i]=1;}
      for(my $j=$count;$j>=$i+1;$j--){
         if(@BIBKEY[$j] =~ m/@BIBKEY[$i]/ && @BIBKEY[$i] =~ m/@BIBKEY[$j]/){  # Two entries coincide
            @BIBREP[$j]++; if(@BIBREP[$j]<2){@BIBREP[$j]=2}
            @BIBREP[$i]=1;
         }
      }
    }
    for(my $i=1;$i<=$count;$i++){  
      if(@BIBREP[$i]>0){
       if(@DT[$i] =~ m/Collective Volume Article/){@BIBKEY[$i] = @BIBKEY[$i].@BIBREP[$i];}
       else{  # If not In-Collection item, replace numbers by letters whenever number less than 25
        @BIBREP[$i] =~ s/25/z/g; @BIBREP[$i] =~ s/24/y/g; @BIBREP[$i] =~ s/23/x/g; @BIBREP[$i] =~ s/22/w/g; @BIBREP[$i] =~ s/21/v/g;
        @BIBREP[$i] =~ s/20/u/g; @BIBREP[$i] =~ s/19/t/g; @BIBREP[$i] =~ s/18/s/g; @BIBREP[$i] =~ s/17/r/g;  @BIBREP[$i] =~ s/16/q/g; @BIBREP[$i] =~ s/15/p/g; @BIBREP[$i] =~ s/14/o/g; @BIBREP[$i] =~ s/13/n/g; @BIBREP[$i] =~ s/12/m/g; @BIBREP[$i] =~ s/11/l/g;
        @BIBREP[$i] =~ s/10/k/g; @BIBREP[$i] =~  s/9/j/g; @BIBREP[$i] =~  s/8/h/g; @BIBREP[$i] =~  s/7/g/g;  @BIBREP[$i] =~  s/6/f/g; @BIBREP[$i] =~  s/5/e/g; @BIBREP[$i] =~  s/4/d/g; @BIBREP[$i] =~  s/3/c/g; @BIBREP[$i] =~  s/2/b/g; @BIBREP[$i] =~  s/1/a/g; 
        @BIBKEY[$i] = @BIBKEY[$i].'-'.@BIBREP[$i];
       }}
    }
}


print OUT "\n\n" ;


for(my $i=1;$i<=$count;$i++){  # Write to output file

if(@DT[$i] =~ m/Journal Article/){
  @JO[$i] =~ s/Journal of Law Economics and Organization/Journal of Law, Economics, and Organization/g;
  @JO[$i] =~ s/Journal of Money Credit and Banking/Journal of Money, Credit, and Banking/g; 
  if($option =~ m/\-l/)   {print OUT '@ARTICLE{,'. "\n" ;}
  elsif($option =~ m/\-k/){print OUT '@ARTICLE{'.@BIBKEY[$i].','. "\n" ;}
  else                    {print OUT '@ARTICLE{'.$i.','. "\n" ;}
  print OUT 'author =       {' . @AU[$i] . '},' . "\n";
  print OUT 'title =        {' . @TI[$i] . '},' . "\n";
  print OUT 'journal =      {' . @JO[$i] . '},' . "\n";
  print OUT 'year =         {' . @PY[$i] . '},' . "\n";
  print OUT 'volume =       {' . @VO[$i] . '},' . "\n";
  print OUT 'number =       {' . @NO[$i] . '},' . "\n";
  print OUT 'pages =        {' . @PG[$i] . '},' . "\n";
  print OUT 'month =        {' . @MO[$i] . '},' . "\n";
  print OUT 'note =         {' . @RM[$i] . '},' . "\n";
  print OUT 'abstract =     {' . @AB[$i] . '},' . "\n";
  print OUT 'keywords =     {' . @KW[$i] . '},' . "\n";
  print OUT 'source =       {' . @SO[$i] . '},' . "\n";
  if($option =~ m/\-f/){print OUT 'file = F'."\n";}
  print OUT '}'."\n\n";
  }

elsif(@DT[$i] =~ m/Working Paper/){
  if($option =~ m/\-t/){
  @JO[$i] =~ s/\s+Working\s+Paper(s*)\s+in\s+([a-zA-Z\-\'\`]+)\,\s+([a-zA-Z\-\'\`]+)(\,*)\s+and\s+([a-zA-Z\-\'\`]+)//g; @JO[$i] =~ s/\s+Discussion\s+Paper(s*)\s+in\s+([a-zA-Z\-\'\`]+)\,\s+([a-zA-Z\-\'\`]+)(\,*)\s+and\s+([a-zA-Z\-\'\`]+)//g; @JO[$i] =~ s/\s+working\s+paper(s*)\s+in\s+([a-zA-Z\-\'\`]+)\,\s+([a-zA-Z\-\'\`]+)(\,*)\s+and\s+([a-zA-Z\-\'\`]+)//g; @JO[$i] =~ s/\s+discussion\s+paper(s*)\s+in\s+([a-zA-Z\-\'\`]+)\,\s+([a-zA-Z\-\'\`]+)(\,*)\s+and\s+([a-zA-Z\-\'\`]+)//g; 
  @JO[$i] =~ s/\s+Working\s+Paper(s*)\s+in\s+([a-zA-Z\-\'\`]+)\s+and\s+([a-zA-Z\-\'\`]+)//g; @JO[$i] =~ s/\s+Discussion\s+Paper(s*)\s+in\s+([a-zA-Z\-\'\`]+)\s+and\s+([a-zA-Z\-\'\`]+)//g; @JO[$i] =~ s/\s+working\s+paper(s*)\s+in\s+([a-zA-Z\-\'\`]+)\s+and\s+([a-zA-Z\-\'\`]+)//g; @JO[$i] =~ s/\s+discussion\s+paper(s*)\s+in\s+([a-zA-Z\-\'\`]+)\s+and\s+([a-zA-Z\-\'\`]+)//g; 
  @JO[$i] =~ s/\s+Working\s+Paper(s*)\s+in\s+([a-zA-Z\-\'\`]+)//g; @JO[$i] =~ s/\s+Discussion\s+Paper(s*)\s+in\s+([a-zA-Z\-\'\`]+)//g; @JO[$i] =~ s/\s+working\s+paper(s*)\s+in\s+([a-zA-Z\-\'\`]+)//g; @JO[$i] =~ s/\s+discussion\s+paper(s*)\s+in\s+([a-zA-Z\-\'\`]+)//g; 
  @JO[$i] =~ s/\s+Working\s+Paper(s*)//g; @JO[$i] =~ s/\s+Discussion\s+Paper(s*)//g; @JO[$i] =~ s/\s+working\s+paper(s*)//g; @JO[$i] =~ s/\s+discussion\s+paper(s*)//g; 
# (avoid confusion; some institutions have both working papers and technical papers; see NBER) @JO[$i] =~ s/\s+Technical\s+Report(s*)//g; @JO[$i] =~ s/\s+technical\s+report(s*)//g; 
# (avoid confusion; some institutions have both working papers and technical papers; see NBER) @JO[$i] =~ s/\s+Technical\s+Paper(s*)//g; @JO[$i] =~ s/\s+technical\s+paper(s*)//g; 
  @JO[$i] =~ s/\s+series\s+in\s+([a-zA-Z\-\'\`]+)//g;   @JO[$i] =~ s/\s+Series\s+in\s+([a-zA-Z\-\'\`]+)//g; 
  @JO[$i] =~ s/\s+series//g;   @JO[$i] =~ s/\s+Series//g; 
  if($option =~ m/\-l/)   {print OUT '@TECHREPORT{,'. "\n" ;}
  elsif($option =~ m/\-k/){print OUT '@TECHREPORT{'.@BIBKEY[$i].','. "\n" ;}
  else                    {print OUT '@TECHREPORT{'.$i.','. "\n" ;}
  print OUT 'author =       {' . @AU[$i] . '},' . "\n";
  print OUT 'title =        {' . @TI[$i] . '},' . "\n";
  print OUT 'institution =  {' . @JO[$i] . '},' . "\n";
  print OUT 'address =      {' . @AD[$i] . '},' . "\n";
  print OUT 'year =         {' . @PY[$i] . '},' . "\n";
  print OUT 'month =        {' . @MO[$i] . '},' . "\n";
  print OUT 'type =         {' . @TY[$i] . '},' . "\n";
  print OUT 'number =       {' . @VO[$i] . '},' . "\n";
  print OUT 'note =         {' . @RM[$i] . '},' . "\n";
  print OUT 'abstract =     {' . @AB[$i] . '},' . "\n";
  print OUT 'keywords =     {' . @KW[$i] . '},' . "\n";
  print OUT 'source =       {' . @SO[$i] . '},' . "\n";
  if($option =~ m/\-f/){print OUT 'file = F'."\n";}
  print OUT '}'."\n\n";
   }
  else {
  if($option =~ m/\-l/)   {print OUT '@ARTICLE{,'. "\n" ;}
  elsif($option =~ m/\-k/){print OUT '@ARTICLE{'.@BIBKEY[$i].','. "\n" ;}
  else                    {print OUT '@ARTICLE{'.$i.','. "\n" ;}
  print OUT 'author =       {' . @AU[$i] . '},' . "\n";
  print OUT 'title =        {' . @TI[$i] . '},' . "\n";
  print OUT 'journal =      {' . @JO[$i] . '},' . "\n";
  print OUT 'note =         {' . @RM[$i] . '},' . "\n";
  print OUT 'year =         {' . @PY[$i] . '},' . "\n";
  print OUT 'volume =       {' . @VO[$i] . '},' . "\n";
  print OUT 'month =        {' . @MO[$i] . '},' . "\n";
  print OUT 'abstract =     {' . @AB[$i] . '},' . "\n";
  print OUT 'keywords =     {' . @KW[$i] . '},' . "\n";
  print OUT 'source =       {' . @SO[$i] . '},' . "\n";
  if($option =~ m/\-f/){print OUT 'file = F'."\n";}
  print OUT '}'."\n\n";
  }
  }

elsif(@DT[$i] =~ m/Collective Volume Article/){
  if($option =~ m/\-l/)   {print OUT '@INCOLLECTION{,'. "\n" ;}
  elsif($option =~ m/\-k/){print OUT '@INCOLLECTION{'.@BIBKEY[$i].','. "\n" ;}
  else                    {print OUT '@INCOLLECTION{'.$i.','. "\n" ;}
  print OUT 'author =       {' . @AU[$i] . '},' . "\n";
  print OUT 'title =        {' . @TI[$i] . '},' . "\n";
  print OUT 'pages =        {' . @PG[$i] . '},' . "\n";
  print OUT 'booktitle =    {' . @BT[$i] . '},' . "\n";
  print OUT 'publisher =    {' . @PB[$i] . '},' . "\n";
  print OUT 'year =         {' . @PY[$i] . '},' . "\n";
  print OUT 'editor =       {' . @ED[$i] . '},' . "\n";
  print OUT 'volume =       {' . @VO[$i] . '},' . "\n";
  print OUT 'number =       {' . @NO[$i] . '},' . "\n";
  print OUT 'series =       {' . @SE[$i] . '},' . "\n";
  print OUT 'type =         {' . @TY[$i] . '},' . "\n";
  print OUT 'chapter =      {' . @CH[$i] . '},' . "\n";
  print OUT 'address =      {' . @AD[$i] . '},' . "\n";
  print OUT 'edition =      {' . @EN[$i] . '},' . "\n";
  print OUT 'month =        {' . @MO[$i] . '},' . "\n";
  print OUT 'note =         {' . @RM[$i] . '},' . "\n";
  print OUT 'isbn =         {' . @IB[$i] . '},' . "\n";
  print OUT 'abstract =     {' . @AB[$i] . '},' . "\n";
  print OUT 'keywords =     {' . @KW[$i] . '},' . "\n";
  print OUT 'source =       {' . @SO[$i] . '},' . "\n";
  if($option =~ m/\-f/){print OUT 'file = F'."\n";}
  print OUT '}'."\n\n";
  }

elsif(@DT[$i] =~ m/Book/ && @DT[$i] !~ m/Book Review/){
  if($option =~ m/\-l/)   {print OUT '@BOOK{,'. "\n" ;}
  elsif($option =~ m/\-k/){print OUT '@BOOK{'.@BIBKEY[$i].','. "\n" ;}
  else                    {print OUT '@BOOK{'.$i.','. "\n" ;}

  if(@AU[$i] =~ s/\,\s+ed[s]*\.*//){ 
        # print OUT 'Got an editor '. "$i \n";
        print OUT 'editor =       {' . @AU[$i] . '},' . "\n";}
  else{ print OUT 'author =       {' . @AU[$i] . '},' . "\n";}
  print OUT 'title =        {' . @TI[$i] . '},' . "\n";
  print OUT 'publisher =    {' . @PB[$i] . '},' . "\n";
  print OUT 'year =         {' . @PY[$i] . '},' . "\n";
  print OUT 'volume =       {' . @VO[$i] . '},' . "\n";
  print OUT 'number =       {' . @NO[$i] . '},' . "\n";
  print OUT 'series =       {' . @SE[$i] . '},' . "\n";
  print OUT 'address =      {' . @AD[$i] . '},' . "\n";
  print OUT 'edition =      {' . @EN[$i] . '},' . "\n";
  print OUT 'month =        {' . @MO[$i] . '},' . "\n";
  print OUT 'note =         {' . @RM[$i] . '},' . "\n";
  print OUT 'isbn =         {' . @IB[$i] . '},' . "\n";
  print OUT 'abstract =     {' . @AB[$i] . '},' . "\n";
  print OUT 'keywords =     {' . @KW[$i] . '},' . "\n";
  print OUT 'source =       {' . @SO[$i] . '},' . "\n";
  if($option =~ m/\-f/){print OUT 'file = F'."\n";}
  print OUT '}'."\n\n";
  }

elsif(@DT[$i] =~ m/Dissertation/){
  if($option =~ m/\-l/)   {print OUT '@PHDTHESIS{,'. "\n" ;}
  elsif($option =~ m/\-k/){print OUT '@PHDTHESIS{'.@BIBKEY[$i].','. "\n" ;}
  else                    {print OUT '@PHDTHESIS{'.$i.','. "\n" ;}
  print OUT 'author =       {' . @AU[$i] . '},' . "\n";
  print OUT 'title =        {' . @TI[$i] . '},' . "\n";
  print OUT 'school =       {' . @SC[$i] . '},' . "\n";
  print OUT 'year =         {' . @PY[$i] . '},' . "\n";
  print OUT 'type =         {' . @TP[$i] . '},' . "\n";
  print OUT 'address =      {' . @JO[$i] . '},' . "\n";
  print OUT 'month =        {' . @MO[$i] . '},' . "\n";
  print OUT 'note =         {' . @RM[$i] . '},' . "\n";
  print OUT 'abstract =     {' . @AB[$i] . '},' . "\n";
  print OUT 'keywords =     {' . @KW[$i] . '},' . "\n";
  print OUT 'source =       {' . @SO[$i] . '},' . "\n";
  if($option =~ m/\-f/){print OUT 'file = F'."\n";}
  print OUT '}'."\n\n";
  }

} # End for() loop for printing

if($noprbdt==0){print OUT "\n" .'%The AEA EconLit .dat file did not contain any DT: entry.'. "\n" ; warn 'The AEA EconLit .dat file did not contain any DT: entry.'. "\n". 'Error occured';}
