Heffstar has asked for the wisdom of the Perl Monks concerning the following question:

Good Afternoon Monks,

After beating my head against my screen for a little while, I figured I'd ask those who know significantly more than me.

I've created a recursive subroutine that provides me with some results (according to the warn commands, I'm getting what I'm looking for) but the issue is that my storage array is empty when it's finished. I'm sure it's something "simple", but I can't seem to figure it out. Here's the code:

my @level; &myfunc($number, $company, \@level, 0, $dbh); print $#level; # prints -1 sub myfunc { my ($manager_no, $company_id, $level_ref, $index, $dbh) = @_; my ($employee_no, $emp_company_id); my ($sql, $sth); my @level = @$level_ref; $sql = 'query'; $sth = $dbh->prepare($sql); if ( !($sth->execute($manager_no, $company_id)) ) { die; } while ( ($employee_no, $emp_company_id) = $sth->fetchrow_array() ) + { if ( exists $level[$index]{"$employee_no-$emp_company_id"} ) { $level[$index]{"$employee_no-$emp_company_id"} = 1; } else { if ( ( $employee_no eq $manager_no) && ($emp_company_id eq $co +mpany_id) ) { # do nothing } else { $level[$index]{"$employee_no-$emp_company_id"} = "$manager_no- +$company_id"; &myfunc($employee_no, $emp_company_id, $level_ref, $index + 1, + $dbh); } } } }

If someone has a suggestion, I'd be very happy to hear it.

Thanks and all the best!

Hugh

Replies are listed 'Best First'.
Re: Recursion - Array of Hashes
by toolic (Bishop) on Feb 02, 2010 at 19:14 UTC
    I don't think it has anything to do with recursion. I think it has to do with how you are using your array reference. In the sub, you are copying the array, and modifying only the copy. I created a small example to illustrate what might be going on:
    use strict; use warnings; use Data::Dumper; my @levs; myfunc(\@levs); print "$#levs\n"; print Dumper(\@levs); sub myfunc { my ($aref) = @_; my @levs = @$aref; $levs[0] = 'foo'; print 'in sub ', Dumper(\@levs); } __END__ in sub $VAR1 = [ 'foo' ]; -1 $VAR1 = [];
    Compare that with:
    sub myfunc { my ($aref) = @_; $aref->[0] = 'foo'; print 'in sub ', Dumper($aref); }
Re: Recursion - Array of Hashes
by ikegami (Patriarch) on Feb 02, 2010 at 19:16 UTC

    You never change myfunc's @$level_ref, thus you get no changes to the file's @level

    Maybe you think my @level = @$level_ref; creates an alias? No, it creates a new array.

      That's exactly what I was thinking I was doing. Thanks for clearing it up!

Re: Recursion - Array of Hashes
by crashtest (Curate) on Feb 02, 2010 at 19:28 UTC

    Just briefly scanning your code, it looks like you're working with a new array in your subroutine instead of the array reference you passed into it. When you do this:

    my @level = @$level_ref;
    ... you've essentially taken a copy of the array that $level_ref was pointing to. Then whenever you assign via $level[$index], you're working with the copy that's scoped to your subroutine - and not with the original @level that's scoped for the entire script.

    There are two ways you could approach this. One way is to continue down the path you're trying to get on, and using $level_ref in your subroutine. In a nutshell, you'd replace code that was working with the subroutine scoped @level with code that uses the passed-in array reference $level_ref:

    $level[$index]{"$employee_no-$emp_company_id"} = 1; # ... becomes ... $level_ref->[$index]{"$employee_no-$emp_company_id"} = 1;

    Another way to solve the issue is to just work directly with the script-scoped @level, and forget about passing the reference around. This isn't exactly clean (it's the "global variable" approach), but hey, if it gets the job done... all you'd have to do is remove the line that declares @level within the subroutine:

    my @level = @$level_ref; # delete this!

    Hope this helps.

      Yes, I see exactly what you mean. I was hoping to not have globals, so I'll stick with the referencing, but I think I'll try to do it properly.

        After doing it properly, it works beautifully. I just wish the data I was working with wasn't all ... screwy. Thanks for all the help!