Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > replace words in string with hash values

Reply
Thread Tools

replace words in string with hash values

 
 
wana
Guest
Posts: n/a
 
      11-29-2004
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)
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-29-2004
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
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      11-29-2004
"wana" <> wrote in message
news: om...
> 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

 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      11-29-2004
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,
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-30-2004
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
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-30-2004
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
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-30-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Replace stop words (remove words from a string) BerlinBrown Python 6 01-17-2008 02:37 PM
split camelcase string into array of words words pantagruel Javascript 8 07-22-2006 07:46 PM
replace words with bold words Lasse Edsvik ASP General 9 10-07-2003 01:19 PM
Replace words into a string with a space before or after Francesco Moi Perl Misc 3 08-20-2003 01:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57