OK, I'm going to assume that I can modify your Category class. If not, you can simply derive a new class from it, and place my additions in that.

First, lets stop using that nasty "field" method. We can simply define:

sub name { shift->field('name') } sub sub_cat_ids { @{shift->field('sub_cats') }
That'll get rid of a bit of the line noise.

Next, I want a more powerful 'new' operator. To avoid confusion, I'll name it new_tree. Later, we may rename it back to "new":

sub new_tree { my $class = shift; my $id = shift; my $depth = shift; my $self = $class->new($id); $self->{_depth} = $depth; $self->{_sub_cats} = [ sort { $a->name cmp $b->name } map { $class->new_tree($_, $depth+1) } $self->sub_cat_ids ]; return $self; } sub sub_cats { @{shift->{_sub_cats} }
With these methods in place, we can add one final method to return the flattened list:
sub as_hash { my $self = shift; my $name = $self->name; my $depth = $self->depth; my $id = $self->id; return { depth->$depth, id=>$id, name=>$name }; } sub as_flattened_list { my $self=shift; return $self->as_hash, map { $_->as_flattened_list } $self->sub_cats }
Finally, we need to modify your BuildCatList function to use the upgraded Category class:
sub BuildCatList { my $top = Category->new_tree("top", 0); my @cats_list = $top->as_flattened_list; my $cats = &main::GetDB('cats'); $cats->{'~cat_list'} = &main::nfreeze( \@cat_list ); }
I've omitted all your undef checks; you can add them back if you want. --Dave.

In reply to Re: Re: Re: Printing a dir tree by dpuu
in thread Printing a dir tree by ClickToViewInfo

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.