Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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:
FOO MOC SEC BAR COM COM
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:
COM.COMPUTER.SEC.FOO / COM.SECT / MOC

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

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (14)
    As of 2014-12-29 13:38 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (188 votes), past polls