bradcathey has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Cleaner code to build hierarchical display of page names
by BrowserUk (Patriarch) on May 17, 2005 at 06:37 UTC | |
|
Re: Cleaner code to build hierarchical display of page names
by mrborisguy (Hermit) on May 17, 2005 at 03:33 UTC | |
by bradcathey (Prior) on May 17, 2005 at 11:46 UTC | |
by holli (Abbot) on May 17, 2005 at 11:54 UTC | |
|
Re: Cleaner code to build hierarchical display of page names
by dragonchild (Archbishop) on May 17, 2005 at 03:30 UTC | |
|
Re: Cleaner code to build hierarchical display of page names
by jhourcle (Prior) on May 17, 2005 at 11:26 UTC | |
|
Re: Cleaner code to build hierarchical display of page names
by danb (Friar) on May 17, 2005 at 15:08 UTC |