Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
•Update: I've retracted this post, it doesn't apply to the situation (I missed the part about it being db records that need to be updated) This stuff below is only useful if you need to do the substitutions in a string.

use strict; use warnings; my %replace = qw( COM SEC MOC COM SEC COM CO MOC ); my $searchpat = join '|', sort { length $b <=> length $a } keys %repla +ce; $searchpat = qr/$searchpat/; # optional, dunno if it speeds up things my $example = "FOO CO COM BAR MOC SEC"; $example =~ s/($searchpat)/$replace{$1}/g; print "$example\n";
Produces the output:
As you can see, it also handles circularity without problem; COM <-> SEC.

Note that I'm sorting the keys on length to make sure longer patterns match first (otherwise "COM" would become "SECM").

If the search-string needs to occur as a word by itself, use \b, like:
my $example = "MOC.COMPUTER.COM.FOO / SEC.SECT / CO"; $example =~ s/\b($searchpat)\b/$replace{$1}/g; print "$example\n";
which prints:

Note that in this case sorting on the length of the key is not necessary, so you can simply do my $searchpat = join '|', keys %replace;

Final note: this code assumes the keys don't contain any odd chars like regex metachars. Since your example shows all-uppercase abbreviations, so apparently this isn't a problem. If it is, then you'll need to look into using quotemeta.


In reply to Re: Ordering hash replacements to avoid clobbering things by xmath
in thread Ordering hash replacements to avoid clobbering things (update chaining) by grinder

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    [choroba]: Good morning!

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (6)
    As of 2018-06-21 07:36 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (117 votes). Check out past polls.