if ( $q->param() ) { open (FH, "$path_to_text") or die "where's the damn file? : $!"; # also set this in earlier part to match $/="\n\n\n"; #set the input Record seperator to a TPIPLE newline my @updated_text; while () { next if /^\n+$/; #ignore lines which contains only newlines $post_number++; # ignore deleted next if ( $q->param("box$post_number") ); # keep the rest push @updated_text, $_; } close(FH); # now update file open(FH,">$path_to_text") || die $!; print FH join "\n\n\n", @updated_text; close(FH); # show success here... }