[ Please do not top-post your replies. TIA ]
sanket vaidya wrote:
Here is the entire code to accomplish your task. It will delete 1st & 3rd
lines.
use warnings;
use strict;
my @array;
open FH,"data.txt";
You should *always* verify that the file opened correctly:
open FH, '<', 'data.txt' or die "Cannot open 'data.txt' $!";
@array = <FH>;
my @array = <FH>;
for my $i (0..$#array)
{
$array[$i] =~ s/^(\*\/tmp\/dst\/file(1|3)\*(\d){3}\*RW\*(\d){3,4})$/ /;
If you have a lot of / characters in the pattern you should probably use
a delimiter other than /. A character class is better for single
character alternatives then using alternation and you don't use the
captured strings so you don't need capturing parentheses.
$array[$i] =~ s!^\*/tmp/dst/file[13]\*\d{3}\*RW\*\d{3,4}$! !;
#replace the lines you want to delete with " " (space)
}
my @result = grep(/[^\s]/,@array); #Take other lines in @result.
[^\s] could be written more simply as \S.
Or you could just remove the elements from the original array:
for my $i ( reverse 0 .. $#array ) {
splice @array, $i, 1
if $array[$i] =~ m!^\*/tmp/dst/file[13]\*\d{3}\*RW\*\d{3,4}$!;
}
close FH;
open FH,">data.txt";
You should *always* verify that the file opened correctly:
open FH, '>', 'data.txt' or die "Cannot open 'data.txt' $!";
print FH @result;
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall