Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Recursion - Array of Hashes

by crashtest (Curate)
on Feb 02, 2010 at 19:28 UTC ( #821013=note: print w/replies, xml ) Need Help??

in reply to Recursion - Array of Hashes

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.

Replies are listed 'Best First'.
Re^2: Recursion - Array of Hashes
by Heffstar (Acolyte) on Feb 02, 2010 at 20:22 UTC

    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!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://821013]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2017-08-19 20:18 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (312 votes). Check out past polls.