TheBigAmbulance has asked for the wisdom of the Perl Monks concerning the following question:
Thank you to the PerlMonks for taking the time to examine this newcomers question. I have a Putty CSV created from Putty Session Manager in Windows. I want to populate a XML that stores sessions in the KDE application 'konsole'. So I need to convert a CSV to a XML that konsole will understand. Thanks to others at comp.lang.perl.misc, I have gotten this far. Here is my code:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $infile = '/media/Docs/Scripts/Perl/Putty/TEST.csv'; open (CSVFILE, $infile) || die ("Could not open $infile! $!"); my @final; while ( my $line = <CSVFILE> ) { $line =~ tr/"\r\n//d; my @cleanline = split /,/, $line; my ( undef, @prefinal ) = split /\\/, $cleanline[ 1 ]; push @final, [ @prefinal, @cleanline[ 0, 3 ] ]; } close $infile; my %hash; foreach my $leaf (@final) { my $ptr = \%hash; foreach my $i ( 0 .. $#$leaf - 1 ) { my $node = $leaf->[$i]; if( $i != $#$leaf - 1 ) { $ptr->{$node} = {} unless( exists $ptr->{$node} ); $ptr = $ptr->{$node}; } else { $ptr->{$node} = $leaf->[$i + 1]; } } } print qq(<SESSION>\n); foreach my $k1 (sort keys %hash) { print qq( <$k1>\n); foreach my $k2 (sort keys %{$hash{$k1}}) { print qq( <$k2>\n); foreach my $k3 (sort keys %{$hash{$k1}{$k2}}) { print qq( <$k3>\n); print qq( $hash{$k1}{$k2}{$k3}\n); print qq( </$k3>\n); } print qq( </$k2>\n); } print qq( </$k1>\n); } print qq(</SESSION>\n); exit(0);
The input in the csv file is as follows:
"Genview-EMS","Sessions\NOC-CO\Servers","","172.16.19.254" "Mayberry (external)","Sessions\NOC-CO\Servers","","216.196.75.8" "Mayberry (internall)","Sessions\NOC-CO\Servers","","172.16.18.103" "OPIE2 (SUN NTP SERVER)","Sessions\NOC-CO\Servers","","172.16.18.102" "10008PKRV_L","Sessions\NOC-INT\Core\Rotuers\Console","","donp:7001@10 +.10.17.68" "10008PKRV_R","Sessions\NOC-INT\Core\Rotuers\Console","","donp:7002@10 +.10.17.68" "6509PKRV_L","Sessions\NOC-INT\Core\Rotuers\Console","","donp:7003@10. +10.17.68" "6509PKRV_R","Sessions\NOC-INT\Core\Rotuers\Console","","donp:7004@10. +10.17.68"
The output generated from the script is (NOTED: Not worthy for konsole yet):
<SESSION> <NOC-CO> <Servers> <Genview-EMS> 172.16.19.254 </Genview-EMS> <Mayberry (external)> 216.196.75.8 </Mayberry (external)> <Mayberry (internall)> 172.16.18.103 </Mayberry (internall)> <OPIE2 (SUN NTP SERVER)> 172.16.18.102 </OPIE2 (SUN NTP SERVER)> <co-gateway> 10.70.64.33 </co-gateway> </Servers> </NOC-CO> <NOC-INT> <Core> <Rotuers> HASH(0x2100ec8) </Rotuers> </Core> </NOC-INT> </SESSION>
Note: The Routers portion of the XML has more elements, but it is represented by 'HASH(0x2100ce8)' because the foreach loops do not go deeper into the hashes. The whole issue is that my element count is not standard, and is variable. So if I want to print a hash that contains 4 elements, I need 3 foreach loops. If I want to print out a hash that contains 8 elements, I would need 7 foreach loops. My fear and inexperience makes me think I would need to get to the last leaf of the hash in order to calculate how many foreach loops I would need. This would defeat the entire purpose.
Does someone have a chance to give me some advice on what you think I should do?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Veriable Length Array/Hash derived from CSV to populate an XML
by wind (Priest) on Mar 29, 2011 at 19:00 UTC | |
by TheBigAmbulance (Acolyte) on Mar 29, 2011 at 19:22 UTC | |
by wind (Priest) on Mar 29, 2011 at 19:31 UTC | |
|
Re: Veriable Length Array/Hash derived from CSV to populate an XML
by Tux (Canon) on Mar 30, 2011 at 06:08 UTC |