CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to convert Hash table to CSV file

 



zeta_man
New User

Jun 8, 2015, 2:13 AM

Post #1 of 3 (1419 views)
How to convert Hash table to CSV file Can't Post

Hi all expert,

I have used a variable $xml to store the dumper hash table below:

my $xml = Dumper %($collectresult);

And below is the content when I run "print dumper $xml;


Code
 
$VAR1 = '$VAR1 = \'result\';
$VAR2 = {
\'item\' => {
\'server2.company.com\' => {
\'key\' => \'1\',
\'property\' => \'Sales\',
\'site\' => \'India\',
\'model\' => \'FSS-4\',
\'id\' => \'1333333\',
},
\'server1.company.com\' => {
\'key\' => \'0\',
\'property\' => \'Sales\',
\'site\' => \'Singapore\',
\'model\' => \'FSS-5\',
\'id\' => \'1333334\',

}
}
};
$VAR3 = \'meta\';
$VAR4 = {
\'total_pages\' => \'1\',
\'current_page\' => \'1\',
\'per_page\' => \'1\',
\'order_by\' => \'system.node.node_id\',
\'total\' => \'2\'
};
';



Could you provide the sample to me how to get the content from $VAR2 from $xml into an array and then use it to write the result into the CSV file like the format below?

server2.company.com,SALES,india
server1.company.com,SALES,Singapore


BillKSmith
Veteran

Jun 8, 2015, 5:40 AM

Post #2 of 3 (1414 views)
Re: [zeta_man] How to convert Hash table to CSV file [In reply to] Can't Post

The code you posted could not have produced the output that you show. You told Dumper to dump a hash. The output is the dump of list. It would have been clearer if you posted the result of:

Code
print Dumper $collectresult;


The code below extracts the required data from my best guess of your data structure.



Code
use strict; 
use warnings;
use Data::Dumper;
my $collectresult = [
'result',
{
item => {
'server2.company.com' => {
key => 1,
property => 'Sales',
site => 'India',
model => 'FSS-4',
id => 1333333,
},
'server1.company.com' => {
key => 0,
property => 'Sales',
site => 'Singapore',
model => 'FSS-5',
id => 1333334,
},
},
},
'meta',
{
total_pages => 1,
current_page => 1,
per_page => 1,
order_by => 'system.node.node_id',
total => 2
},
];
my $xml = Dumper @{$collectresult};

print Dumper $xml;

while (my ($server, $company) = each %{$collectresult->[1]->{item}}) {
print "$server,$company->{property},$company->{site}\n";
}

Good Luck,
Bill


Laurent_R
Veteran / Moderator

Jun 8, 2015, 10:20 AM

Post #3 of 3 (1408 views)
Re: [zeta_man] How to convert Hash table to CSV file [In reply to] Can't Post

Do you want to convert your hash to XML or to CSV? It's not clear to me.

And, in either case, but especially for a CSV, you need to specify the rules for the conversion. Your hash is a nested data structure, which a CSV is made of flat records. Conversion cannot be done without specific rules as to how to represent the nested structure.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives