Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2021-08-03 07:59 GMT
Find Nodes?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)

    Results (35 votes). Check out past polls.