I didn't know quite where to start with this. Your convertCsvToHash() code clearly does not do what you are expecting. There are some modules that can do this rather well, but below is my hint in a straightforward way. Indentation is important and is great impediment to reading your code. I have never used the XML writer code but hopefully solving the CSV part will help you out.

Update: Note to the OP: You provided simple example data, but perhaps too simple. The kind of problems that can crop up in these CSV files are legion. "Fred Smith, MD", "George Bush, Jr" or "Widget, INC". Even parsing phone numbers can be a whole can of worms. Parsing a full blown CSV file is better done with a module because the details are a lot harder than one might think. However, you have another big problem, the XML. But now maybe you can focus on getting XML working and then come back to the CSV part when you discover that neither one of our simple approaches will work with complex data. BTW, kudos to you for providing code that actually compiles and runs albeit with the "wrong' result.

use strict; use warnings; use XML::Writer; use IO::File; use Data::Dumper; my $CSVfile='1.csv'; my ($arrRef,@keys)= convertCsvToHash($CSVfile); print Dumper $arrRef; print Dumper \@keys; sub convertCsvToHash { my $file = shift; my @AoH; open(FILE, "$CSVfile") or die "$!";; my $header = <FILE>; chomp $header; my @keys = split ',',$header; while (my $data=<FILE>) { my %hash; chomp $data; my @values = split ',',$data; foreach my $key (@keys) { $hash{$key} = shift @values; } push @AoH, \%hash; } return \@AoH,@keys; } __END__ $VAR1 = [ { 'Phone' => '1111', 'Name' => 'A', 'Company' => 'X', 'Email' => 'a.x@aol.com' }, { 'Email' => 'b.y@aol.com', 'Company' => 'Y', 'Name' => 'B', 'Phone' => '0' }, { 'Email' => 'c.z@aol.com', 'Phone' => '2222', 'Name' => 'C', 'Company' => 'Z' } ]; $VAR1 = [ [ 'Name', 'Company', 'Email', 'Phone' ] ];

In reply to Re: csv to xml by Marshall
in thread csv to xml by vavz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.