in reply to Generating HTML from a Hashref of Hashrefs

I liked holli's post but I just didn't like the format of your input file. If you want structure that both a program and a human can work with, you need XML:

Your data file, transformed a bit:

<?xml version="1.0" ?> <BASENODE> <HEAD name="Benefits"> <Desc> your benefits</Desc> <Subcategories> <category name="Employee Recognition"> <Url> http://blah.com/recognition/index.html</Url> <topics> <topic name="Employee Service Awards"> <Url> http://blah.com/recognition/employee_service.html</Url +> </topic> <topic name="General Recognition and Performance-based Awards" +> <Url> http://blah.com/recognition/awards.html</Url> </topic> </topics> </category> <category name="Equity Plans"> <Url> Overridden Regional Content URL</Url> <topics> <topic name="Incentive Plan"> <Url> http://blah.com/equity_plans/incentive/override.html</ +Url> </topic> </topics> </category> </Subcategories> <Url> http://blah.com/index.html</Url> </HEAD> <HEAD name="Compensation"> <Subcategories /> <Url> http://blah.com/comp/index.html</Url> </HEAD> <HEAD name="Corporate Policies"> <Subcategories /> <Url> http://blah.com/corp_policies/index.html</Url> </HEAD> <HEAD name="Early Retirement"> <Subcategories /> <Url> http://blah.com/ero/index.html</Url> </HEAD> </BASENODE>

Code using XML::Simple:

#!/usr/bin/perl -w use strict; use XML::Simple; use Data::Dumper; local $/; open(H,'subcontent2.xml') or die $!; my $wholefile =<H>; close(H); my $xmlref = XMLin($wholefile, ForceArray=>['topic']); foreach(keys %{$xmlref->{'HEAD'}}){ if (!$xmlref->{HEAD}->{$_}->{Subcategories}->{category}){ print qq|GENERAL URL($_): | . $xmlref->{HEAD}->{$_}->{Url} . qq|\n +|; } else { foreach my $cat($xmlref->{HEAD}->{$_}->{Subcategories}->{category} +){ for (keys %$cat){ print qq|CATEGORY URL($_): | . $cat->{$_}->{Url} +. qq|\n|; foreach my $topics ( values %{$cat->{$_}->{topics} +}){ foreach(keys %{$topics}){ print qq|TOPIC URL($_): | . $topics->{$_}->{Url} . qq| +\n|; } } } } } } #print Dumper $xmlref; 1;

Spits out:

perl subcontent.pl GENERAL URL(Corporate Policies): http://blah.com/corp_policies/index. +html GENERAL URL(Compensation): http://blah.com/comp/index.html GENERAL URL(Early Retirement): http://blah.com/ero/index.html CATEGORY URL(Employee Recognition): http://blah.com/recognition/index +.html TOPIC URL(Employee Service Awards): http://blah.com/recognition/emplo +yee_service.html TOPIC URL(General Recognition and Performance-based Awards): http://b +lah.com/recognition/awards.html CATEGORY URL(Equity Plans): Overridden Regional Content URL TOPIC URL(Incentive Plan): http://blah.com/equity_plans/incentive/ove +rride.html

What's my point? By using XML (possibly with a schema or a DTD) you will notice whether or not your nested data is well-formed. A tabbed text file as a data source is likely to cause you trouble down the road.

Celebrate Intellectual Diversity

Replies are listed 'Best First'.
Re^2: Generating HTML from a Hashref of Hashrefs
by rje (Deacon) on Oct 10, 2005 at 01:50 UTC
    I.S.:

    As I mentioned, the input structure was not the issue at hand; however, perhaps I did not make it clear enough that the targetted input structure is, in fact, XML, and yes we will be using XML::Simple. I am currently using YAML for development because I am very familiar with it.

    And in fact, I have encapsulated the configuration reading and writing, so you see, changing the input format really is of no consequence at all.