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?


In reply to Veriable Length Array/Hash derived from CSV to populate an XML by TheBigAmbulance

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.