markdibley has asked for the wisdom of the Perl Monks concerning the following question:

Hello

Today I needed to turn values in a csv into a chunk of XML (required for an XLST transformation). Because the data in the XML is nested severa layers deep, the csv header describes the location in the XML like a file directory

#id layer/first layer/second id1 value1 value2 id2 value3 value4

The only way I could work out how to build a branch and attach a leaf in a hash was the following. From there I can use XML::Simple to generate the XML. If there was an easier way I would love to know it

#!/usr/bin/perl -w use strict; use Data::Dumper; my $list1 = "this/is/a/twig"; my $list2 = "this/is/a/branch"; my $list3 = "this/is/another/branch"; my $hash = {}; my $hashroot = $hash; my @hashtags1 = split(/\//, $list1); my @hashtags2 = split(/\//, $list2); my @hashtags3 = split(/\//, $list3); attach_leaf($hash,\@hashtags1, "leaf1"); attach_leaf($hash,\@hashtags2, "leaf2"); attach_leaf($hash,\@hashtags3, "leaf3"); print Dumper($hashroot); sub attach_leaf{ my ($tree, $branch, $leaf) = @_; # Work along the branch for(my $i = 0; $i < scalar(@$branch); $i++) { # build branch if($i != (scalar(@$branch) - 1)){ # build new branch if it doesn't exist already $tree->{$$branch[$i]} = {} if(! exists $tree->{$$branch[$i +]}); $tree = $tree->{$$branch[$i]}; } # attach leaf else{ $tree->{$$branch[$i]} = $leaf; return $hash; } } }
output was
$VAR1 = { 'this' => { 'is' => { 'a' => { 'branch' => 'leaf2', 'twig' => 'leaf1' }, 'another' => { 'branch' => 'leaf3' } } } };

Replies are listed 'Best First'.
Re: Building a tree 1 leaf at a time
by choroba (Cardinal) on Jan 12, 2012 at 23:54 UTC
    Using XML::XSH2:
    use XML::XSH2; @list = qw(this/is/a/twig this/is/a/branch this/is/another/branch); xsh 'create this'; xsh "set $_" for @list; xsh 'ls .';
Re: Building a tree 1 leaf at a time
by delirium (Chaplain) on Jan 12, 2012 at 20:21 UTC
    The solution you came up with does seem to work... but how did the header file get generated in the first place? Why is a flatfile being generated to reference an XML file? That just seems wrong.

    But more importantly, why do you not want to use what you came up with? Is it slow? What size files are we talking about?

      The file comes from an excel file compiled by someone in an office. The header has to be manually corrected. It is the only format people are qualified to generate. As for why I asked - because I know in Perl there is more than one way and it looks like there is something new to learn if I can wrap my head round XSH2. Thanks for the reply delirium and thanks for the suggestion choroba.
Re: Building a tree 1 leaf at a time
by locked_user sundialsvc4 (Abbot) on Jan 13, 2012 at 14:00 UTC

    “There’s More Than One Way To Do It,™” yes yes, but there is also much to be said for “get ’er done.”   You seem to have developed a one-page solution to your problem that does not require a five-hundred pound gorilla (XSH2) to ride along with you.   I feel that I could read it in a couple minutes and (if I had read it) convince myself that it was correct.   If it were me, I’d take what you now have, and move along.