You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Viljo Marrandi <vi...@inspiral.net> on 2001/09/19 14:57:37 UTC

array problem

Hello,

In apache/mod_perl handler i use recursive function to parse a tree and
find it's root (umm, uppermost ID). If script goes one level up it pushes
it's ID to array, after it's finisthed I pop last ID from array.  Now
first time it runs it works just fine, but after that array contains only
first ID that was pushed there. What could cause this? To make it little
clearer I add table and script. So, first time @maks_id is 18 17 16 1 and
$temp_id = 1, but later array it just 18 and id is 18 too, I don't like
it :(. Oh, and if someone has any good ideas how to improve this tree
parsing algorithm then i'd be happy to hear about it :).


table:

mysql>  SELECT categ_id, parent FROM categories;
+------------+--------+
|   categ_id | parent |
+------------+--------+
|          1 |      0 |
|         16 |      1 |
|         17 |     16 |
|         18 |     17 |
+------------+--------+

program:

sub maksimum {
   my ( $dbh, $kat ) = @_;
   my $counter = 0;
   my @maks_id = [ ];

   my $query = qq{ SELECT categ_id, parent, nimetus FROM categories };
   my $sth = $dbh->prepare_cached( $query ) or die $log->notice($dbh->errstr);
   $sth->execute;
   my $info = $sth->fetchall_arrayref;
   $sth->finish;

   foreach $counter (0 .. $#{$info}) {
      if($info->[$counter][0] == $kat) {
         push @maks_id, $info->[$counter][0];
         &print_it($info->[$counter][1]);
      }
   }

   sub print_it {
      my $counter2 = 0;
      my $counter3 = 0;

      foreach $counter2 (0 .. $#{$info}) {
         if($info->[$counter2][0] == $_[0]) {
            push @maks_id, $info->[$counter2][0];
            foreach $counter3 (0 .. $#{$info}) {
               if($info->[$counter3][0] == $info->[$counter2][1]) {
                  &print_it($info->[$counter3][0]);
               }
            }
         }
      }
   }
   my $temp_id = pop @maks_id;
   return $temp_id;
}


---------------------
- Viljo Marrandi    -
- programmer/admin  -
- Inspiral Network  -
- www.inspiral.net  -
---------------------