Sorry, there currently is no switch statement in Perl. It's coming in Perl 6, but for now you'll have to use a cascading if .. elsif .. elsif .. else configuration. (And yes, it really is spelled elsif.)
However, your code looks like you could use a loop and some kind of data structure.
my @Data =
(
{person => "CC", who => "My name ", func => \&p_CC },
{person => "AB", who => "AB ", func => \&p_AB },
{person => "CD", who => "friend", func => \&p_CD },
);
PERSON:
foreach $person ( keys %house )
{
foreach ( @Data )
{
if ( $_->{ person } eq $person )
{
print $_->{ who };
&{ $_ }->{ func }();
next PERSON;
}
}
# Handle else here.
}
If the person in
%House is found, the print and function call will be executed, and we'll continue with the next key in the loop. Otherwise, we exhaust the possibilities of the table and fall through to the bottom of the inner loop.
And I would put the most likely array elements near the top (if possible) in order to reduce the execution time.
That's it!
--t. alex
"Of course, you realize that this means war." -- Bugs Bunny.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.