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

Hi monks

I am doing a small coding for copying the sub folders to another location with some condition checks.

In that i have one small condition,

if ($total < 7)) { push (@final, @temp); $hash{$s++}= \@final; @final =(); }

In the @temp array i have some values and i will push it in another @final array if the condition satisfies. I stored the array reference in a hash for my later use.When i printed the hash key and values i didnt got any output. I found where i am going wrong, that is, I am again nullifying the array @final in the block.

As per my coding i want to nullify that array if the condition satisfies. Also i want to store the array reference in the hash. I tried using temporary array but i could not able to get the solution.

So is there anyother way to store the array reference in the hash and nullify that array later.

Thanks in advance

Prasad

Replies are listed 'Best First'.
Re: array reference
by davido (Cardinal) on Jan 15, 2005 at 16:16 UTC

    You've got a couple of problems here that are interrelated.

    First, each time you execute the statement, $hash{$s++} = \@final; you're using the same array @final...unless @final is a lexical that falls out of scope between each iteration.

    Second is a consequence of the first... When you wipe out @final, you're obliterating the content of the array referred to by the hash element you just set.

    You should use either (or both) lexical scoping to ensure that @final is a new instance each time you take a reference to it, and/or you should be using the anonymous array constructor to generate a reference to the data held in the array, rather than to the array itself. That means doing:

    $hash{$s++} = [ @final ];

    Instead of

    $hash{$s++} = \@final;

    You should definately actively seek to learn and understand lexical scoping as discussed in perlintro and perlsub, as well as the difference between taking a reference, and creating an anonymous array (see perlreftut and perlref, and perllol). I hope this helps...


    Dave

      Thanks for all the suggestions given by gems. I have got the solution after changing into anonymous array reference.

      Prasad

Re: array reference
by holli (Abbot) on Jan 15, 2005 at 07:24 UTC
    if ($total < 7)) { push (@final, @temp); $hash{$s++}= [@final]; @final =(); }

    holli, regexed monk
Re: array reference
by demerphq (Chancellor) on Jan 15, 2005 at 11:58 UTC

    Im betting you arent using lexicals here. I think in all my time programming perl I've written something like @final =(); only a handful of times. Normally you dont need to clear arrays as scoping takes care of it for you. Without seeing more code its hard to say for sure but the code you have posted makes me think that your problem isn't really the one you think it is.

    For instance if @final were lexically declared inside of a loop then taking a reference to it would be just fine as each pass through the loop the @final would actually be a different array.

    ---
    demerphq

Re: array reference
by CountZero (Bishop) on Jan 15, 2005 at 10:43 UTC
    May assumptions may be totally wrong (in which case just disregard what I'm saying), but assuming $s is a simple counter, you would be better off by storing the references in an array instead of in a hash. If the keys to your hash are sequential integers starting with zero, an array is less overhead and faster.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law