Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use feature 'switch';
- # Author: David Ellinger
- # Date: 10/14/2011
- # Phonebook Program
- my $choice = 0;
- my $name;
- my $phonenumber;
- my $phoneBookFile = "phonebook.dat";
- my %phonebook = ();
- open(PBF, "<",$phoneBookFile) or die ("Could not open $phoneBookFile.");
- while (my $line = <PBF>){
- chomp $line;
- %phonebook = (%phonebook,(split(/\t/,$line))); # I want to seperate them by tabs
- }
- while($choice != 5){
- $choice = menu();
- given($choice){
- when(1){
- addEntry();
- }
- when(2){
- deleteEntry();
- }
- when(3){
- print("Look up a Person **NOT DONE**\n");
- }
- when(4){
- print("List all the Persons\n");
- listEntries();
- }
- when(5){
- print("Quitting the Program...\n");
- }
- default{
- die("I don't know what to do with $choice");
- }
- }
- }
- sub menu{
- while(1){
- print("1. Add an Entry");
- print("\n2. Delete an Entry");
- print("\n3. Look up a Person");
- print("\n4. List all the Persons");
- print("\n5. Quit");
- print("\n\nYour choice: ");
- $choice = <stdin>;
- chomp $choice;
- if ($choice =~ m/^\s*[1-5]\s*$/){
- return $choice;
- }else{
- print("##########################\nThis input is not correct.\n##########################\n");
- print("Please enter a number between 1 and 5.\n");
- }
- }
- }
- sub listEntries{
- foreach my $key (keys(%phonebook)){
- print("$key | $phonebook{$key}\n"); # Need to fix this.
- }
- }
- sub addEntry{
- print("Adding an Entry\n");
- print("Enter the Name you would like to enter: ");
- my $newName = <STDIN>;
- print("Enter the Phone Number you would like to enter: ");
- my $newNum = <STDIN>;
- chomp $newName;
- chomp $newNum;
- $phonebook{$newName} = $newNum;
- }
- sub deleteEntry{ #Not Deleting correctly
- print("Delete an Entry\n");
- print("Enter the Name or Number you would like to delete: ");
- my $delEntry = <STDIN>;
- delete $phonebook{$delEntry};
- }
Add Comment
Please, Sign In to add comment