use Tie::File; #.......your code #at this point you have #@ports with port numbers #and file address.txt which you need to append tie @addresses, 'Tie::File', "address.txt" or die $!; for my $position (0..$#ports) { $addresses[$position] .= $ports[$position]; } untie @addresses;