in reply to file substitution

I am not sure on performance, as I have not implemented something like this, but...

How about something like this....

... my @junk = map {chomp $_; qr(\Q$_\E)} <JUNK>; while (<>) { # Added / Modified my $line = $_; next if $line =~ $_ foreach (@junk); # End update print; }

Order the junkfile in descending order of occurances.

Another option, to avoid the inner loop, would be to build a giant alternation regexp (qr((foo|bar|biz|bang))) from the junk file. However, then you need to escape various things. In addition, I am not sure which is faster, the alternation regexp or the nested loop.

--MidLifeXis

P.S. All code is untested, blah blah blah.

Update: Updated foreach stuff - Thanks BrowserUk. Added \Q..\E. Thanks Berik.

Replies are listed 'Best First'.
Re^2: file substitution
by BrowserUk (Patriarch) on Jun 04, 2004 at 18:56 UTC

    This foreach my $junk (@junk); isn't valid perl syntax.

    If you want to name the control variable you have to use the foreach my $name (@things) { #use $name }, not the postfix form


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
Re^2: file substitution
by Berik (Sexton) on Jun 04, 2004 at 18:46 UTC
    Be sure to use quotemeta or \Q..\E
    my @junk = map {chomp $_; qr/\Q$_\E/} <JUNK>; while (<>) { next if $_ =~ $junk foreach my $junk (@junk); print; }
    perl's regexes are not as fast as grep's. But this will do, probably fast enough.
    ---
    Berik