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


In reply to Re: Generating HTML from a Hashref of Hashrefs by InfiniteSilence
in thread Generating HTML from a Hashref of Hashrefs by rje

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.