Guest User

Untitled

a guest
Jun 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.14 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use feature 'switch';
  6.  
  7.  
  8. # Author: David Ellinger
  9. # Date: 10/14/2011
  10.  
  11. # Phonebook Program
  12.  
  13. my $choice = 0;
  14.  
  15. my $name;
  16. my $phonenumber;
  17. my $phoneBookFile = "phonebook.dat";
  18. my %phonebook = ();
  19.  
  20.  
  21. open(PBF, "<",$phoneBookFile) or die ("Could not open $phoneBookFile.");
  22. while (my $line = <PBF>){
  23.     chomp $line;
  24.     %phonebook = (%phonebook,(split(/\t/,$line))); # I want to seperate them by tabs
  25. }
  26.  
  27.  
  28. while($choice != 5){
  29.    $choice = menu();
  30.    given($choice){
  31.       when(1){
  32.          addEntry();          
  33.       }
  34.       when(2){
  35.          deleteEntry();
  36.       }
  37.       when(3){
  38.          print("Look up a Person **NOT DONE**\n");
  39.       }
  40.       when(4){
  41.          print("List all the Persons\n");
  42.          listEntries();
  43.       }
  44.       when(5){
  45.          print("Quitting the Program...\n");
  46.       }
  47.       default{
  48.          die("I don't know what to do with $choice");
  49.       }
  50.    }
  51. }
  52.      
  53.  
  54.  
  55. sub menu{
  56.   while(1){
  57.       print("1. Add an Entry");
  58.       print("\n2. Delete an Entry");
  59.       print("\n3. Look up a Person");
  60.       print("\n4. List all the Persons");
  61.       print("\n5. Quit");
  62.       print("\n\nYour choice: ");
  63.       $choice = <stdin>;
  64.       chomp $choice;
  65.       if ($choice =~ m/^\s*[1-5]\s*$/){
  66.          return $choice;
  67.       }else{
  68.          print("##########################\nThis input is not correct.\n##########################\n");
  69.          print("Please enter a number between 1 and 5.\n");
  70.       }
  71.    }
  72. }
  73.  
  74. sub listEntries{
  75.    foreach  my $key (keys(%phonebook)){
  76.       print("$key | $phonebook{$key}\n"); # Need to fix this.
  77.    }
  78. }
  79.  
  80. sub addEntry{
  81.    print("Adding an Entry\n");
  82.       print("Enter the Name you would like to enter: ");
  83.       my $newName = <STDIN>;
  84.       print("Enter the Phone Number you would like to enter: ");
  85.       my $newNum = <STDIN>;
  86.       chomp $newName;
  87.       chomp $newNum;
  88.       $phonebook{$newName} = $newNum;
  89. }
  90.  
  91. sub deleteEntry{ #Not Deleting correctly
  92.    print("Delete an Entry\n");
  93.    print("Enter the Name or Number you would like to delete: ");
  94.    my $delEntry = <STDIN>;
  95.    delete $phonebook{$delEntry};
  96. }
Add Comment
Please, Sign In to add comment