Fellow Monasterians,

I'm building a web page hierarchy 'tree' from a db table that conveniently stores the relationships between the pages (allows users to see the site make up), I've gotten it to work, but the code looks crazy bloated. Any way to golf this and make it shorter? faster? cleaner? but still readable? Thanks!

my $sqldata = [ { pageid => 1, level => 1, under => 0, pagename => 'Groups' }, { pageid => 2, level => 1, under => 0, pagename => 'About' }, { pageid => 5, level => 3, under => 4, pagename => 'Tea' }, { pageid => 4, level => 2, under => 1, pagename => 'Womens' }, { pageid => 6, level => 4, under => 5, pagename => 'Registration' } +, { pageid => 7, level => 2, under => 1, pagename => 'Mens' }, { pageid => 8, level => 3, under => 4, pagename => 'Retreat' } ]; #level = hierarchy, #under = page that this page falls under my @tree; for my $i ( 0 .. $#$sqldata ) { if ($sqldata->[$i]{under} == 0 ) { push (@tree, $sqldata->[$i]); } for my $j ( 0 .. $#$sqldata ) { if ($sqldata->[$j]{under} == $sqldata->[$i]{pageid} && $sqldata- +>[$j]{level} == 2) { $sqldata->[$j]{tab} = 1; push (@tree, $sqldata->[$j]); for my $k ( 0 .. $#$sqldata ) { if ($sqldata->[$k]{under} == $sqldata->[$j]{pageid} && $sq +ldata->[$k]{level} == 3) { $sqldata->[$k]{tab} = 2; push (@tree, $sqldata->[$k]); for my $l ( 0 .. $#$sqldata ) { if ($sqldata->[$l]{under} == $sqldata->[$k]{pageid} +&& $sqldata->[$l]{level} == 4) { $sqldata->[$l]{tab} = 3; push (@tree, $sqldata->[$l]); } } } } } } } for ( 0 .. $#tree ) { if ( $tree[$_]{tab} == 1 ) { print "---" } if ( $tree[$_]{tab} == 2 ) { print "------" } if ( $tree[$_]{tab} == 3 ) { print "---------" } print $tree[$_]{pagename}."<br />"; } OUTPUT: Groups ---Womens ------Tea ---------Registration ------Retreat ---Mens About

Update: fixed typos


—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

In reply to Cleaner code to build hierarchical display of page names by bradcathey

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.