In the same vein I'd advise the OP to constrain the scope of his variables to make it easier to see where they are used and to avoid the bogus initialisations.

Always initialising variables seems like a good idea, but is only appropriate where the variable is being given a default value. If it is intended that the initial value will always be replaced, it is much better not to provide a bogus intial value. Perl's use warnings; "undef used" warning gives an immediate heads up that there is a path or condition in your code that you hadn't anticipated is you leave variables uninitialised until you have a sensible value for them.

Reworking the code again with that in mind and after fixing various issues that arise during testing you might end up with:

#!/usr/bin/perl use strict; use warnings; my @nme; # Input loop while ((print "Enter the name: "), defined (my $input = <STDIN>)) { chomp $input; last if ! length $input; # Exit loop on empty line my ($fn, $ln) = split /\s+/, $input, 2; if (! defined $fn) { print "Blank name ignored\n"; next; } $ln ||= '_missing_'; # Provide missing name print "$fn, $ln\n"; push @nme, "$ln, $fn"; } # Print loop print "$_\n" foreach sort @nme;

Note too the use of the comma operator in teh while loop expression to get the prompt output each time through the loop. Not a highly recommended technique, but can be less ugly and more cogent than many of the alternative ways of achieving the same effect.

Note too that the hash changed into an array. It's quite possible for two people to have the same last name. In fact it is even possible for people to have the same name. Names are tricky. ;)


DWIM is Perl's answer to Gödel

In reply to Re^2: Problem in printing lastname and firstname by GrandFather
in thread Problem in printing lastname and firstname 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.