in reply to Example of push
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 |