in reply to Example of push

Your code works as is, except for the \b issue in the last regex, as mentioned by Paladin. Note however, that, unlike what Paladin says (although probably like what he meant to say), is that. \b matches any spot between a \w character (alphanumerics and _, so [A-Za-z0-9_]) and a \W character (anything that is not a \w). In your example, you have put the \b in front of a dash, which is a \W, so the character in front of that would have to be a \w, which it is not (it's a space, in fact). That regex should be /\bskipped/ (or even /\bskipped\b/ if you're paranoid).

You should also heed Paladin's warning re: the array slices. In this case you get the expected result, as your slices result in one element lists, which return the last (and only) element in scalar context.

Another possible improvement is to put your regexes in a hash or an array to avoid the repetitive code you have above. Something like this:

use strict; use Data::Dumper; my @txtfile=("file/path - skipped", "file/path - cannot verify", "file/path - corrupt file", "My camel has fleas", ); my @regex=('\bcannot verify', '\bcorrupt file', '\bskipped', ); my @outfile; foreach my $error (@txtfile) { foreach (@regex) { push (@outfile, $error) if $error=~/$_/; } } print Dumper(\@outfile); __END__ $VAR1 = [ 'file/path - skipped', 'file/path - cannot verify', 'file/path - corrupt file' ];

Update: Turns out that Paladin actually did say what I thought he meant to say :). \w and \W were virtually indistinguishable in the font my browser picked for the <ul>, but he did say \W in the right places.

CU
Robartes-

Replies are listed 'Best First'.
Re: Re: Example of push
by perlguy (Deacon) on Mar 26, 2003 at 16:22 UTC
    Expanding a bit on what robartes said, using a precompiled regular expression (qr//) might speed up the iterative loop. Because $_ is changing through each iteration of the loop:
    $error=~/$_/
    /$_/ must be re-compiled each time the value of $_ changes. Though this won't matter much in this instance, because of the relatively few number of iterations, a better way to do this would be to change the contents of @regex from:
    my @regex=('\bcannot verify', '\bcorrupt file', '\bskipped', );
    to:
    my @regex=( qr/\bcannot verify/, qr/\bcorrupt file/, qr/\bskipped/, );
    or even:
    my @regex = map qr/$_/, ( '\bcannot verify', '\bcorrupt file', '\bskipped', );