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-


In reply to Re: Example of push by robartes
in thread Example of push by dbrock

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.