my @tworows = split /[^\n]+\n[^[n]+\n/, $text; #### s/((\w+).+\1.+\1.+)\n(.+)/something/ #### 1. next; } is the same as } 2. Don't make a copy of your row array to use it, just dereference it: my @row1 = @{$row}; # not needed.. Just use $row->[$index] instead of $row1[$index] 3. drop all the extra parens join ';', @arr; push @arr, 'something'; 4. it looks like you're using a numeric == to compare ascii fields like $row1[0] == $row2[0] when col 0 contains things like 'PF'. Use eq for these compares. You can't use == there.