in reply to Problem with loops

This smells like homework, but I'll help you get started...
The main problem is faulty loop construction.

We need a loop that will end when the user types "end" -( I also added "quit" which would be a more normal "ending command"). So, make that "ending condition" appear prominently as a loop condition. Below I used a "while" loop.

I like to use the comma operator to put the prompting part also inside the loop condition. You don't have to do that, but this avoids having to put the "command prompt" in more than one place.

Most of these things use a short one line prompt rather than repeating all of this long verbiage for each user input. You can add a help function if you like.

Normal user input rules allow leading and trailing white space on a user inputted line. And a blank line is normally just a re-prompt (not an error).

I would use subroutines for the functions. This leads to a very clean looking main loop. I would not worry about cramming things together in the minimum number of lines - make it look nice and easy to understand.

Update:

I see that %directory is not the best name. I used that because that is what you had. %addressBook would be better, as would print_addressBook() instead of print_directory(). Think about good names - this makes a lot of difference!

Perl gets a reputation as "write once" code when we get too cryptic. It doesn't have to be that way! And often these cryptic looking "one-liners" run slower than a more verbose formulation.

I would argue that code like the below is understandable - at least at a high level - to someone skilled in the art of programming even if they don't understand every detail of the Perl syntax.

#!/usr/bin/perl -w use strict; print " Welcome to your own Personal Address Book!\n"; my $explain_text = " Command are: add Add An Entry del Delete An Entry prn Print all Entries end End Program quit Also Ends program "; my $short_prompt = "add,del,prn or quit: "; my $input; my %directory=(); print $explain_text; #don't keep printing this long stuff while ( ($input=prompt_for_input($short_prompt)), $input !~ /\s*end|qu +it\s*$/i) { next if $input =~ /^\s*$/; # re-prompt on blank lines # this is not an error! if ($input =~ /^\s*add\s*$/) { add_entry(); } elsif ($input =~ /^\s*del\s*$/) { delete_entry(); } elsif ($input =~ /^\s*prn\s*$/) { print_directory(); } else { print "illegal command!\n"; } } sub prompt_for_input { my $message = shift; print $message; my $input = <>; $input =~ s/^\s*//; # no leading spaces $input =~ s/\s*$//; # no trailing spaces return $input; } sub add_entry { my $name = prompt_for_input ("Enter the Name you would like to add: + "); my $address = prompt_for_input ("Enter an Address for $name: "); $directory{$name} = $address; } sub delete_entry { print "some code to get name to delete goes here\n"; } sub print_directory { print "These are the people in your address book:\n"; printf "%-15s %s\n", "Name", "Address"; foreach my $who (sort keys %directory) { printf "%-15s %s\n", $who, $directory{$who}; } print "\n"; }