<div dir="ltr"><div><div><div><div><div><div><div><div>I have a simple CSV file with the header row, and a values row.  For a secondary system to process the data I need to massage the CSV into a Key-Value pair file.<br><br>

</div>Example of CSV:<br></div><div style="margin-left:40px"><span style="font-family:courier new,monospace">name,age,gender,zip</span><br><span style="font-family:courier new,monospace"></span><span style="font-family:courier new,monospace">john,33,male,68000</span><br>

</div></div><br></div>What I need is to produce a file with this:<br></div><div style="margin-left:40px"><span style="font-family:courier new,monospace">name=john</span><br><span style="font-family:courier new,monospace">age=33</span><br>

<span style="font-family:courier new,monospace"></span><span style="font-family:courier new,monospace">gender=male</span><br><span style="font-family:courier new,monospace">zip=68000</span><br></div></div><br></div>Using Text::CSV I'm close, but I don't like the code I came up with:<br>

</div><div><div style="margin-left:40px"><span style="font-family:courier new,monospace">my $csv = Text::CSV->new( { binary => 1 } );<br>my $headers = $csv->parse($search_results_csv->getline());</span><br><span style="font-family:courier new,monospace">my @headers2 = $csv->fields();</span><br>

<span style="font-family:courier new,monospace">my $values = $csv->parse($search_results_csv->getline());</span><br><span style="font-family:courier new,monospace">my @values2 = $csv->fields();</span><br><span style="font-family:courier new,monospace">for (my $index = 0; $index < scalar @headers2; $index++ ) {</span><br>

<span style="font-family:courier new,monospace"></span></div></div><div style="margin-left:80px"><span style="font-family:courier new,monospace">printf ("%s=%s\n", $headers2[$index], $values2[$index]);</span><br>

</div><div style="margin-left:80px"><span style="font-family:courier new,monospace"></span></div><div style="margin-left:40px"><span style="font-family:courier new,monospace">}</span><br clear="all"><span style="font-family:courier new,monospace"></span></div>

<div><div><div><div><div><div><div><div><div><div><div><br></div><div>Is there a cleaner way of getting the @headers2 and @values2 array and then walking through both of them in the for loop?<br><br>I thought I could set the header once (using<span class=""></span><span class=""> column_names</span><span class=""></span>() ?) and then pull in the values line and do a foreach on the headers, but I couldn't get that to work.<br>

 <br></div><div>Thoughts or examples?<br><br></div><div>Thanks,<br>Dan<br></div><div><br>-- <br>***************** ************* *********** ******* ***** *** **<br>"Quis custodiet ipsos custodes?"<br>    (Who can watch the watchmen?)<br>

    -- from the Satires of Juvenal<br>"I do not fear computers, I fear the lack of them."<br>    -- Isaac Asimov (Author)<br>** *** ***** ******* *********** ************* *****************
</div></div></div></div></div></div></div></div></div></div></div></div>