Why would you refuse a hash ?
There can't be so much regions as to not easily keep them in memory, in a simple hash like
my %prefixes = ( '04025' = [ 'Region1, Region2, Region3' ],
...
);
And afterwards, a straightforward check like
my ($pref5, $pref4, $pref3, $pref2) = map { substr( $phone, 0, $_ ) }
(5, 4, 3, 2);
my $prefix_length = exists $prefixes{$pref5} ? 5
: exists $prefixes{$pref4} ? 4
: exists $prefixes{$pref3} ? 3
: exists $prefixes{$pref2} ? 2
: 0
;
my $formatted_phone = join( ' ',
substr( $phone, 0, $prefix_length),
substr( $phone, $prefix_length),
);
should work rather
very effectively. If you have thought about this already, why do you think it would be expensive/ineffective/inadequate ?
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.