The problem is that you have work that needs to be done for each element of the address and different work that should be done for specific elements. The solution is to separate the two sets of processing:

use strict; use warnings; print mungeAddress($_) . "\n" for '123 Circle Way', '123 Way Circle'; sub mungeAddress { my %street_suf_lkup = (circle => 'Cir', boulevard => 'Blvd'); my @parts = map { # Apply the global corrections # proper case words, simple $_ = ucfirst lc $_; # if apartments or units, such as #A or #215-C, capitalize + unit # numbers. Don't strip off pound signs. s/^(#.+)/\U$1/; # correct numbering, like 0000048th -> 48th s/^(#?)0+([1-9]\S+)/$1$2/; # correct casing of O'Brien, McDonald, etc. s/^(mc|o')(.+$)/\u\L$1\E\u\L$2/i; # Remove literal dots at the end of elements. s/\.+$//g; $_; } split ' ', $_[0]; # Normalize street prefixes to standard postal abbreviations, for # example Circle -> Cir, Boulevard -> Blvd $parts[-1] = $street_suf_lkup{lc $parts[-1]} if exists $street_suf +_lkup{lc $parts[-1]}; return "@parts"; }

Prints:

123 Circle Way 123 Way Cir

True laziness is hard work

In reply to Re: Counting elements being mapped via map{} by GrandFather
in thread Counting elements being mapped via map{} by furry_marmot

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.