replace words in string with hash values

Discussion in 'Perl Misc' started by wana, Nov 29, 2004.

  1. wana

    wana Guest

    foreach (keys %h)
    {
    $a =~ s/\b$_\b/$h{$_}/g;
    }

    I want to replace matches in string to hash key with hash value. I am
    replacing acronyms with phrases where acronym is hash key. Is there a
    better or different way?

    thanks!

    wana (on pda)
     
    wana, Nov 29, 2004
    #1
    1. Advertising

  2. wana wrote:
    > foreach (keys %h)
    > {
    > $a =~ s/\b$_\b/$h{$_}/g;
    > }
    >
    > I want to replace matches in string to hash key with hash value. I am
    > replacing acronyms with phrases where acronym is hash key. Is there a
    > better or different way?


    You have Perl search the whole string as many times as there are keys in
    the hash. With this approach, searching the string once is sufficient:

    my $keys = join '|', keys %h;
    $a =~ s/($keys)/$h{$1}/g;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Nov 29, 2004
    #2
    1. Advertising

  3. wana

    Paul Lalli Guest

    "wana" <> wrote in message
    news:...
    > foreach (keys %h)
    > {
    > $a =~ s/\b$_\b/$h{$_}/g;
    > }
    >
    > I want to replace matches in string to hash key with hash value. I am
    > replacing acronyms with phrases where acronym is hash key. Is there a
    > better or different way?


    Well, here's one that's different, though not necessarily better (in
    fact, quite likely worse)

    $a=~ s/\b(\B+)\b/$h{$1} or $1/ge;

    Rather than searching the string for each hash key, this one would
    search each word in the string to determine if it is in the hash (more
    correctly stated: if it has a true value in the hash). If so, replace
    it with the hash value, if not, leave it as is.

    Benchmarking is left as an excercise to the OP. ;-)

    One comment, however: Be careful about the use of \b. While \b does
    mean 'word boundary', it means Perl's definition of a 'word', which is:
    [0-9a-zA-Z_]+ That means that "don't" is two words: "don" and "t".
    This may or may not be what you actually want.

    Paul Lalli
     
    Paul Lalli, Nov 29, 2004
    #3
  4. On 29 Nov 2004 12:23:16 -0800, (wana) wrote:

    >foreach (keys %h)
    >{
    > $a =~ s/\b$_\b/$h{$_}/g;
    >}
    >
    >I want to replace matches in string to hash key with hash value. I am
    >replacing acronyms with phrases where acronym is hash key. Is there a
    >better or different way?


    Don't know if it's "better" (depends on far too many different
    things!), but

    s/\b\w+\b/$h{$&}||$&/ge;

    should do the job. Of course if your acronyms follow some convention
    (e.g. 2 to 4 uppercase letters only) it could be improved
    performance-wise:

    s/\b[A-Z]{2,4}\b/$h{$&}||$&/ge;


    HTH,
    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Nov 29, 2004
    #4
  5. Gunnar Hjalmarsson <> wrote:
    > wana wrote:
    >> foreach (keys %h)
    >> {
    >> $a =~ s/\b$_\b/$h{$_}/g;
    >> }
    >>
    >> I want to replace matches in string to hash key with hash value. I am
    >> replacing acronyms with phrases where acronym is hash key. Is there a
    >> better or different way?

    >
    > You have Perl search the whole string as many times as there are keys in
    > the hash. With this approach, searching the string once is sufficient:
    >
    > my $keys = join '|', keys %h;
    > $a =~ s/($keys)/$h{$1}/g;



    But you better put the word boundaries back in though!


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 30, 2004
    #5
  6. Tad McClellan wrote:
    > Gunnar Hjalmarsson wrote:
    >> wana wrote:
    >>>
    >>> foreach (keys %h)
    >>> {
    >>> $a =~ s/\b$_\b/$h{$_}/g;
    >>> }
    >>>
    >>> I want to replace matches in string to hash key with hash value.
    >>> I am replacing acronyms with phrases where acronym is hash key.
    >>> Is there a better or different way?

    >>
    >> You have Perl search the whole string as many times as there are
    >> keys in the hash. With this approach, searching the string once is
    >> sufficient:
    >>
    >> my $keys = join '|', keys %h;
    >> $a =~ s/($keys)/$h{$1}/g;

    >
    > But you better put the word boundaries back in though!


    Yes, they were omitted by mistake; thanks for pointing it out.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Nov 30, 2004
    #6
  7. Bob Walton <> wrote:
    > wana wrote:
    >
    >> foreach (keys %h)
    >> {
    >> $a =~ s/\b$_\b/$h{$_}/g;
    >> }
    >>
    >> I want to replace matches in string to hash key with hash value.


    > The only thing I've got to add to what's already been
    > said is to watch out for regexp metacharacters in your
    > hash keys. I have no idea what you might count as an
    > "acronym". If any metacharacters do appear, you can
    > use \b\Q$_\E\b as your regexp to quote them.



    But if a metachar could be the first or last char, then
    the word boundary probably won't match where you want it to...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 30, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BerlinBrown
    Replies:
    6
    Views:
    4,848
  2. Lasse Edsvik

    replace words with bold words

    Lasse Edsvik, Oct 5, 2003, in forum: ASP General
    Replies:
    9
    Views:
    255
  3. rp
    Replies:
    1
    Views:
    595
    red floyd
    Nov 10, 2011
  4. Francesco Moi
    Replies:
    3
    Views:
    94
    Tad McClellan
    Aug 20, 2003
  5. pantagruel
    Replies:
    8
    Views:
    483
    Dr John Stockton
    Jul 22, 2006
Loading...

Share This Page