I like your idea of an option for regular expressions. I've used that concept below.

Here is the updated code, using your idea of testing for a match before substituting. It seems to do the trick.
my $replacecount = 0; my %linenums; my @keys_ordered = reverse sort { length $table_ref->{$a} <=> length $ +table_ref->{$b} } keys %{$table_ref}; LINE: while ( my $old_line = <INFILE> ) { my $new_line = $old_line; my $old_count = 0; my $new_count = 0; KEY: for my $key ( @keys_ordered ) { #if ($debug) { print "DEBUG: \$key = $key\n" } my $backtrack_line = $new_line; my $regex = ( $opt_regex ? $key : quotemeta($key) ); if ( $old_count = ( $old_line =~ /$regex/g) ) { $new_count = ( $new_line =~ s/$regex/$table_ref->{$key}/g +); if ( $new_count != $old_count ) { if ($debug) { print "DEBUG: Match count failure for k +ey $key on ($old_count/$new_count) on line $.: $old_line" } $new_count = $old_count; $new_line = $backtrack_line; next KEY; } if ( $debug ) { print "DEBUG: line $. replacing $key with $table_ref- +>{$key}\n"; $linenums{$.}++; } $replacecount += $new_count; } else { } # do nothing } print OUTFILE $new_line; next LINE; } if ( $debug ) { for my $linenum ( sort keys %linenums ) { if ( $linenums{$linenum} > 1 ) { print "DEBUG: WARNING: line $linenum had $linenums{$line +num} replacements!\n"; } } } print "Made $replacecount replacements in $output_file\n"; print "Done.\n\n";

Thanks!

- Chris Koknat

In reply to Re^2: Algorithm to search and replace on data file, based on a table file? by koknat
in thread Algorithm to search and replace on data file, based on a table file? by koknat

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.