I don't think you are going to get a much better than tweaking the hash solution unless your data is rather nice and can use Limbic~Region's method. I did try another way (build a regex containing the required substitutions and eval it against the data), more to prove it was a non starter than because I thought it would be faster. Code and benchmark for a laugh.

#!/usr/bin/perl use warnings; use strict; use Benchmark; sub simple_hash { seek DATA, 0, 0; while (<DATA>) {last if /^Names/} my %lookup; while (<DATA>) { next if /^\s*$/; last if /^Example List/; chomp; my ($first, $second)=split; $lookup{$first}=$second; } while (<DATA>) { next if /^\s*$/; chomp; my ($name, $rest)=split /\s+/, $_, 2; print $lookup{$name}, "\t", $rest, "\n"; } } sub funky_regex { seek DATA, 0, 0; while (<DATA>) {last if /^Names/} my $regex_string=""; while (<DATA>) { next if /^\s*$/; last if /^Example List/; chomp; my ($first, $second)=split; $regex_string.="s/$first/$second/;"; } local $/; $_=(<DATA>); eval $regex_string; print ; } timethese(5000000, { 'simple_hash' => &simple_hash, 'funky_regex' => &funky_regex } ); __DATA__ Names Guido Meyer Mike Smith Klaus Rothschild Mick Mouse Daffy LeCannard Example List Guido bla1 ... Mike bla2 ... Klaus bla3 ... Mick blahsome more Daffy lookout Duck ! # results funky_regex: 0 wallclock secs ( 0.30 usr + 0.00 sys = 0.30 CPU) @ 16666666.67/s (n=5000000) (warning: too few iterations for a reliable count) simple_hash: 0 wallclock secs ( 0.01 usr + 0.00 sys = 0.01 CPU) @ 500000000.00/s (n=5000000) (warning: too few iterations for a reliable count)

Cheers,
R.


In reply to Re: exchange words in text - how not to !! by Random_Walk
in thread exchange words in text by Murcia

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!
  • 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, details, 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, summary, 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.