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"; }

In reply to Re: Problem with loops by Marshall
in thread Problem with loops by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.