#!/usr/local/bin/perl
#Usage: perl phone.pl phonedb.txt ariacode.txt
#Entries in the first argument are of the form
#Mary Smith (607) 255-0010  (607)  342-0019
#John Peter  (507) 675-7241  (607) 255-5555
#The second file will contain the names of each person,
#this time last name first, and the aria codes for each
#the person's phone numbers

if (open(inh,"<$ARGV[0]")) {
  if (open(outh,">$ARGV[1]")) {
      while ($myline=<inh>) {
         $myline=~m/^\s*(\w+)\s+(\w+)/;
         $newline="$2".' '."$1";
         #$i=0; 
         while ($myline=~m/\((\d{3})\)\s*\d{3}-\d{4}/g) {
             $newline=$newline.' '."$1";
             #$i+=1; 
         }
         #print "$i\n";
         print outh "$newline\n";
      }  
      close(inh);
      close(outh);
  }
  else {
      print "Error opening $ARGV[1]\n";
      close(inh); 
      exit(1);
  } 
}
else {
   print "Error opening $ARGV[0]\n";
   exit (1);
}
