in reply to foreach - adding newlines?

Just a little nitpick (nothing to do with your problem).  In Perl,

@array = (@array,$d1_value,$d2_value);

would typically be written as

push @array, $d1_value, $d2_value;