in reply to Re (tilly) 1: Replace all at once
in thread Replace all at once

Here's an excerpt from chapter 7 (substitution):
Example 7.4 Sorting text by length for use in a regex.
$keys = join '|', # 4 map { quotemeta } # 3 sort { length($b) <=> length($a) } # 2 keys %change; # 1
  1. We get the list of keys from the hash – these are the strings...
  2. We sort the keys by their length, so that the longest strings come first...
  3. We run them through the quotemeta() function to make them safe for regexes...
  4. And then we join the regex-safe strings with "|" in between them, for regex alternation.
Doing a benchmark, I see that the reverse sort LIST method is (as one would imagine) faster than the sort { length($b) <=> length($a) } LIST method (by more than two times). I'll incorporate your code, tilly, if'n you don't mind. :)

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: Re: Re (tilly) 1: Replace all at once
by dragonchild (Archbishop) on Dec 10, 2001 at 19:26 UTC
    Why didn't you orcish the sort { length($b) <=> length ($a) }?

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      I was considering doing optimizations, but I didn't want to cloud the meaning. I'm going to do a reverse-sort now, anyways.

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;