Problem with Mail merge in perl

This could be a simple problem for the perl experts

I am trying mail merge in perl

The header file is head1 which is as under

,,ACTION,TRANS_ID,,TBL,ForwardDeals_Id,DealStatus,InputMode,CaptureDate,TradeDate,Users_Id,Users_Id_Last,Folders_Id,Purposes_Id,TypeOfEvent,BlockNumber,TypeOfInstr_Id,Comments,Pairs_Id,Amount1,Amount2,

The file containing the values is csv which is as under

,,"INS",973795,,ForwardDeals,883797,V,I,"04/05/2009,04/05/2009,63768,0,63761,61987,K,887591,58935,"649978-booked,52006,-475000.0,23607500.0
,,"INS",973796,,ForwardDeals,883798,V,I,"04/05/2009,04/05/2009,63768,0,63761,61987,K,887592,58935,"649978-booked,52006,-575000.0,33607500.0
,,"INS",973797,,ForwardDeals,883799,V,I,"04/05/2009,04/05/2009,63768,0,63761,61987,K,887593,58935,"649978-booked,52006,-478000.0,23608500.0

The perl script is to merge the head and csv is

#!/usr/bin/perl
while ($f = (head1)) {
open (FH, $f) or die "Can't open $f : $!";
while (<FH>) {
chomp;
push @contents, $;
@contents = split(/,/);
}
while (<>) {
chop() ;
push @values, $
;
@values = split(/,/);
$size = @values;
}
for ($i = 0 ;$i < $size ; $i++) {
printf "$contents[$i] $values[$i] \n" ;
}
}

I am able to get the merge for 1st row of the csv file and the head1 as under by executing

perl mail_merge.pl csv

ACTION "INS"
TRANS_ID 973795

TBL ForwardDeals
ForwardDeals_Id 883797
DealStatus V
InputMode I
CaptureDate "04/05/2009
TradeDate 04/05/2009
Users_Id 63768
Users_Id_Last 0
Folders_Id 63761
Purposes_Id 61987
TypeOfEvent K
BlockNumber 887591
TypeOfInstr_Id 58935
Comments "649978-booked
Pairs_Id 52006
Amount1 -475000.0
Amount2 23607500.0

I am not able to merge the head with the subsequent rows of csv .The desired output would be

ACTION "INS"
TRANS_ID 973795

TBL ForwardDeals
ForwardDeals_Id 883797
DealStatus V
InputMode I
CaptureDate "04/05/2009
TradeDate 04/05/2009
Users_Id 63768
Users_Id_Last 0
Folders_Id 63761
Purposes_Id 61987
TypeOfEvent K
BlockNumber 887591
TypeOfInstr_Id 58935
Comments "649978-booked
Pairs_Id 52006
Amount1 -475000.0
Amount2 23607500.0

ACTION "INS"
TRANS_ID 973796

TBL ForwardDeals
ForwardDeals_Id 883798
DealStatus V
InputMode I
CaptureDate "04/05/2009
TradeDate 04/05/2009
Users_Id 63768
Users_Id_Last 0
Folders_Id 63761
Purposes_Id 61987
TypeOfEvent K
BlockNumber 887592
TypeOfInstr_Id 58935
Comments "649978-booked
Pairs_Id 52006
Amount1 -575000.0
Amount2 33607500.0

Please guide ,Thanks in advance

So you need to read the first line in as a header array and then on reading subsequent lines into an array write each header field and record field

perl -e 'open DATA, "<", "tmp.dat";
$record=readline(DATA);
@FieldNames=split/,/,$record;
while(<DATA>){
   @fields=split/,/,$_;
   for (0..$#fields){
      print "$FieldNames[$_]: $fields[$_]\n" if $fields[$_];
   }
   print "\n","="x80,"\n";
}'

Thanks Skrynesaver

I appended the csv to the head1 and renamed it as tmp.dat and got the desired output

But is there a way out in perl that the head and the csv are in two different files and still I get the output

Sure, read the field names from head1 rather than off the top of the file and keep them as 2 separate arrays

Thanks again

I have tried as suggested , but am unable to get the first record of the csv file in the merge

The script is

#!/usr/bin/perl
open HEAD, "<", "head1";
$contents=readline(HEAD);
@FieldNames=split/,/,$contents;
open DATA, "<", "csv";
$record=readline(DATA);
while(<DATA>){
@fields=split/,/,$;
for (0..$#fields){
print "$FieldNames[$
]: $fields[$]\n" if $fields[$];
}
print "\n","="x80,"\n";
}

The output is
ACTION: "INS"
TRANS_ID: 973796
TBL: ForwardDeals
ForwardDeals_Id: 883798
DealStatus: V
InputMode: I
CaptureDate: "04/05/2009
TradeDate: 04/05/2009
Users_Id: 63768
Folders_Id: 63761
Purposes_Id: 61987
TypeOfEvent: K
BlockNumber: 887592
TypeOfInstr_Id: 58935
Comments: "649978-booked
Pairs_Id: 52006
Amount1: -575000.0
Amount2: 33607500.0

================================================================================
ACTION: "INS"
TRANS_ID: 973797
TBL: ForwardDeals
ForwardDeals_Id: 883799
DealStatus: V
InputMode: I
CaptureDate: "04/05/2009
TradeDate: 04/05/2009
Users_Id: 63768
Folders_Id: 63761
Purposes_Id: 61987
TypeOfEvent: K
BlockNumber: 887593
TypeOfInstr_Id: 58935
Comments: "649978-booked
Pairs_Id: 52006
Amount1: -478000.0
Amount2: 23608500.0

================================================================================

The first record ie ForwardDeals_Id: 883798 is not merged , Please guide

this line outside the loop is reading the DATA file, drop that line and yiou should be there.

$record=readline(DATA);

Thanks very much ,it is working