in reply to Counting elements being mapped via map{}

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