Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: csv to xml

by Marshall (Canon)
on Aug 06, 2016 at 01:06 UTC ( [id://1169236]=note: print w/replies, xml ) Need Help??


in reply to csv to xml

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' ] ];

Replies are listed 'Best First'.
Re^2: csv to xml
by Not_a_Number (Prior) on Aug 06, 2016 at 10:28 UTC
    my @values = split ',',$data;

    Unfortunately, this breaks if one of the 'values' itself contains a comma. And commas are not uncommon in company names.

    Use eg Text::CSV.

      But Text::CSV will not help very much with the type of data shown in the original post, will it?

      AFAIK, it will also break on an additional comma unless there is some additional information, such as quotes, to help identify that the additional comma is not a field separator.

        Yes.

        If the CSV is well-formed, internal commas will be escaped in some way, eg:

        Ed Smith, "Smith, Brown & Jones", sbj@example.com, "999,999,999"

        Of course, if it isn't well-formed, I agree that Text::CSV will not help any more than a hand-rolled CSV parsing kludge...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1169236]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-03-29 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found