A more interesting 'nother way to do it' is by dispatching methods. It's still a hash lookup under the hood, but is a fine technique to be aware of for some situations (adding verbs to a parser for example). Consider:

use warnings; use strict; print "What is your name?\n"; chomp (my $name = ucfirst <STDIN>); my $obj = bless {}; my $call = $obj->can ("name$name"); if ($call) { $call->(); } else { print "I can't handle anyone by the name $name.\n"; } sub nameLarry { print "Hi Larry. Welcome\n"; } sub nameMoe { print "Hi Moe. Late again I see!\n"; } sub nameTim { print "Hi TIMTOWTDI. Still pursuing the alternative life style?\n" +; }

The OP should note that while using an array and grep is an alternative technique, it's not a very good alternative because it doesn't scale well. That is, if there are a lot of names and the lookup needs to be done many times the 'linear search' that grep uses will slow processing down substantially. A small improvement can be made by using a for loop and using last to exit the loop as soon as a match is found, but that really doesn't redeem the array lookup where a hash or method dispatch could have been used.


True laziness is hard work

In reply to Re^2: elsif failing by GrandFather
in thread elsif failing by rcd^_-

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.