On Sat, 18 Jan 2003, Paul Makepeace wrote:
> On Sat, Jan 18, 2003 at 02:02:41AM +0000, Shevek wrote:
> > What is the simplest logic which will construct a tree such as that
> > below. You may use any data representation you like for anything. I happen
> > to be using an SQL database which I read into a hash of (id => array of
> > children), but this isn't important.
>
> Here's one I prepared earlier. It's recursive. It doesn't print pretty
> lines, just totals directory sizes to find bloaty areas. Maybe you want
> pretty lines? There was a thread on FWP recently about this if you care
> to look back.
No, the pretty lines are the whole point. This is for a 'site map' thing
online. My current version contains _three_ major decision points and
feels totally wrong. The decision points are the conditional in the map,
the tr/// and the 'unless $size' in the loop below that. Three major
constructs for performing something this simple really seems like too
much.
Mason follows.
<%method show>
<%args>
$cat
$children
$depth
$mask
</%args>
<%perl>
return if $depth >= 10;
my @prefix = map { ($mask & (1<<$_)) ? '|' : ' ' } (0..($depth-1));
$prefix[-1] =~ tr/ |/`+/ if @prefix;
my $prefix = join(" ", @prefix);
$prefix =~ s/\s+/ /g;
</%perl>
<tt><% $prefix %></tt><% $cat->link_heading %> (<% $cat->entries %> items)<br>
% $mask |= (1 << $depth);
% if ($children->{$cat->id}) {
% my $size = @{ $children->{$cat->id} };
% foreach (sort { $a->title cmp $b->title } @{ $children->{$cat->id} }) {
% $size--;
% $mask &= ~(1<<$depth) unless $size;
<& SELF:show,
cat => $_,
children => $children,
depth => $depth + 1,
mask => $mask,
&>
% }
% }
</%method>
S.
--
Shevek
I am the Borg.
sub AUTOLOAD{my$i=$AUTOLOAD;my$x=shift;$i=~s/^.*://;print"$x\n";eval
qq{*$AUTOLOAD=sub{my\$x=shift;return unless \$x%$i;&{$x}(\$x);};};}
foreach my $i (3..65535) { &{'2'}($i); }