in reply to Regex help/condensation

If you store regexps in your hash rather than just the string you might have an easier time:
my %def = ('triangles?' => '?'); s/\s+$//; # trailing s/^\s+//; # leading s/\s+/ /g; # multiple spaces to a single space s/\b$i\b/$def{$i}/gi;
The \b will make it match on word boundaries.

Updated: fixed stupid mistake...

gav^

Replies are listed 'Best First'.
Re: Re: Regex help/condensation
by Kanji (Parson) on Feb 15, 2002 at 04:43 UTC

    I have to admit I'd prolly have used s/\s+/ /g;, too, but it's something tr is more suited to and reputedly faster for.

    tr/ / /s;

    Update: I had the time, so figured 'what the heck' ...

    Benchmark: running s, tr, each for at least 5 CPU seconds ... s: 5 wallclock secs ( 5.15 usr + 0.01 sys = 5.16 CPU) @ 54047.31/s (n=278722) tr: 5 wallclock secs ( 5.05 usr + 0.00 sys = 5.05 CPU) @ 130668.19/s (n=659613) Rate s tr s 54047/s -- -59% tr 130668/s 142% --

        --k.