use strict; use warnings; open (IN1, "output_file.vhd") or die; ##### some types of code safer. open (IN3, "; chomp @nets; my @enabled_nets = ; chomp @enabled_nets; my $lines = ""; ##### Defined here (I will call it (1)) while () { ##### cuddle your braces or not. Be consistent. #print OUT; ##### (1) should be defined here instead, for ##### the smallest scope. foreach my $i (0..$#nets) { if (s/\=\>\s+$nets[$i]\,/\=\>$enabled_nets[$i]\,/) { my $lines = $_; ##### and here (I will call it (2)) ##### Make it '$lines = $_' (no 'my') ##### and you will probably see your ##### expected results. print "done"; print $lines; ##### prints the value of (2) } } print OUT "$lines"; ##### prints the value of (1) } close (IN1); close (IN2); close (IN3); close (OUT);