in reply to Cleaner code to build hierarchical display of page names

wow... this is impressive, i like it!

here's some thoughts, i don't think they're that great, but some things for you to think about.
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]); } } } } } } }
i didn't look at this too much, but it looks like you do the same thing a bunch of times nested. this may be a prime place for some recursion!



for ( 0 .. $#tree ) { if ( $tree[$_]{tab} == 1 ) { print "---" } if ( $tree[$_]{tab} == 2 ) { print "------" } if ( $tree[$_]{tab} == 3 ) { print "---------" } print $tree[$_]{pagename}."<br />"; }
can be changed to:
for ( 0 .. $#tree ) { print "---" x $tree[$_]{tab}; print $tree[$_]{pagename]."<br />"; }


i like this program, i'm very impressed. i would say with a little recursion and a little abstraction on the second part (instead of testing each value and printing the number of lines according to the test) you should be able to make this so you can have more than just 3 or 4 levels too.

Replies are listed 'Best First'.
Re^2: Cleaner code to build hierarchical display of page names
by bradcathey (Prior) on May 17, 2005 at 11:46 UTC

    Yeah, I apologize for my print routine, which I just threw together to test my results. I did wonder how to use the tab value to avoid the if's. Didn't realize you could multiply a print statement! Ahhhhh, Perl!


    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
      That's no multiplication. It's a repition.

      From perlop:
      Binary ``x'' is the repetition operator. In scalar context or if the left operand is not enclosed in parentheses, it returns a string consisting of the left operand repeated the number of times specified by the right operand. In list context, if the left operand is enclosed in parentheses, it repeats the list.
      # print row of dashes print '-' x 80; #tab over print "\t" x ($tab/8), ' ' x ($tab%8); # a list of 80 1's @ones = (1) x 80; # set all elements to 5 @ones = (5) x @ones;
      Update: Fixed formatting


      holli, /regexed monk/