http://qs321.pair.com?node_id=11115310

agnes00 has asked for the wisdom of the Perl Monks concerning the following question:

Hello guys, I've a file (with a lot of string lines of this format .*?;.*?;.*?;.*?;.*?;.*?;.*?;.*?;.*?;.*?(?:$)); I want to check those who start with the same first word after the first semicolon, then delete one of them from the file (the one that have a particular format). I write this code:
#!/usr/bin/perl use warnings; my $input = 'lines.txt'; open (FILE, "<", $input) or die "Can not open $input $!"; my @lines = <FILE>; close FILE; my @forlines=(); foreach(@lines){ if( ( defined $_) and !($_ =~ /^\s*$/)){ push(@forlines, $_); } } my @buffer = (); foreach $line(@forlines) { $line =~/(.*?);.*?;.*?;.*?;.*?;.*?;.*?;.*?;.*?;.*?(?:$)/; my $var = $1; foreach $line2(@forlines) { $line2 =~/(.*?);.*?;.*?;.*?;.*?;.*?;.*?;.*?;.*?;.*?(?: +$)/; if ($line2 eq $line) { next; } if($var eq $1 and $line2 =~ /(.*?);.*?;SU LI IR ST;.*? +;SU LI IR ST;.*?;.*?;.*?;.*?;.*?(?:$)/) { push @buffer, $line2; } } } foreach $line(@buffer) { @forlines = grep {!/$line/} @forlines; } open(my $file, '>', $input) or die "Could not open file '$input' $!"; truncate $file,0; print $file @forlines;
it works but it is so slow (20 min to formating the file), I think because of the two nested loops. Is there any better solution to do that ? BR