Your nicknames hash seems a little strange because the keys are nicknames, but the values are a mixture of nicknames and formal names. It might be easier to access the data you want if you were to organize it differently. For example, I replaced your "nicknames" hash with a hash named "formal_name":
use warnings; use strict; my %formal_name = ( bill => 'william', will => 'william', abe => 'abraham' ); print_formal_name('will'); print_formal_name('sam'); print_all_nicknames('wiLLiam'); print_all_nicknames('thomas'); sub print_formal_name { my $nickname = shift; if (exists $formal_name{lc $nickname}) { print "The formal name for $nickname is ", ucfirst $formal_nam +e{lc $nickname}, ".\n"; } else { print "There is no formal name for $nickname.\n"; } } sub print_all_nicknames { my $formalname = shift; my @nicknames; for my $nickname (keys %formal_name) { if ($formal_name{$nickname} eq lc $formalname) { push @nicknames, ucfirst $nickname . ', '; } } if (@nicknames) { $nicknames[$#nicknames] =~ s/, //; print ucfirst lc $formalname, " has these nicknames: ", @nickn +ames, ".\n"; } else { print ucfirst lc $formalname, " has no nicknames.\n"; } }
This prints:
The formal name for will is William. There is no formal name for sam. William has these nicknames: Will, Bill. Thomas has no nicknames.

In reply to Re: Help on format or better way to do..? by toolic
in thread Help on format or better way to do..? by learnperl

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.