Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Array mysteriously disappears on loop exit...

by Anonymous Monk
on Oct 09, 2000 at 19:01 UTC ( [id://35911]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I have some code which goes something like this...

package myPackage; my @containerRefArray=(); my @containeeArray=(); ... sub get_parentRelPos (\%$) { my ($object, $name)=@_; my ($i, $j, $found); my @tmp; my $parentObject=$object->{'~PARENT'}; my $objectIndex=$object->{'~entPhysicalIndex'}; for ($i=0; $i < @containerRefArray; $i++) { if ($containerRefArray[$i] eq $parentObject) { $found=1; last; } } ...

I have followed this code through with the debugger, and find that everything is OK until the last; statement, after which @containeeArray contains nothing...
DB<16> x @containeeArray 0 ARRAY(0xf132c0) 0 HASH(0xf11260) 'entPhysicalAlias' => undef ... DB<17> n NIM_Contains::get_parentRelPos(/home/rt106745/dev-edit/modules/NIM_Con +tains.pm:48): 48: if ($found) { DB<17> x @containeeArray empty array


Does anybody here have any clues as to what might be going on?
The arrays contain references to tied hashes, if that's any help, although that shouldn't change anything, should it?

Replies are listed 'Best First'.
(Guildenstern) RE: Array mysteriously disappears on loop exit...
by Guildenstern (Deacon) on Oct 09, 2000 at 19:10 UTC
    First of all, use the CODE tags:
    package myPackage; my @containerRefArray=(); my @containeeArray=(); ... sub get_parentRelPos (\%$) { my ($object, $name)=@_; my ($i, $j, $found); my @tmp; my $parentObject=$object->{'~PARENT'}; my $objectIndex=$object->{'~entPhysicalIndex'}; for ($i=0; $i < @containerRefArray; $i++) { if ($containerRefArray[$i] eq $parentObject) { $found=1; last; } } ...

    Second - based on the code you've given with no other info, it's obvious why your array is empty. I can only assume that somewhere in the snipped portion of your code do you actually do something with @containeeArray, because I don't see it used at all in the code example. Are you using it as a parameter to get_parentRelPos?
    We need more info before we can help!

    Update: Hmmm...Apparently somebody edited the top level node and added CODE tags...

    Guildenstern
    Negaterd character class uber alles!
      Sorry about the formatting - I'm terrible at reading instructions...
      The array does actually contain stuff just prior to the last statement, as it's in the debug output, but I don't know where it goes afterwards.
      Is there any strange behaviour in perk that I'm not aware of - I'm using perl 5.004_04 on Solaris.
      Prior to the call to get_parentRelPos there are many calls to this:
      sub add_containee(\%\%) { my ($container, $containee)=@_; #$container/containeeIndex are references to code providing this infor +mation #find the container my ($object, $i); my $found=0; for ($i=0; $i< @containerRefArray; $i++) { if ($containerRefArray[$i] eq $container) { $found=1; last; } } if (! $found) {push @containerRefArray, $container; push @containeeArray, []} if (defined $containee) { push @{$containeeArray[$i]}, $containee;} }
        Okay, I can see now how you're getting stuff into @containeeArray. I'm still not sure where you're using this. Is it a parameter to get_parentRelPos? I would assume not, since it looks like it should take a hash reference ans a scalar, not an array. Aside from populating the array, tell us how it's used.
        Also, it looks to me like your debugging output references a line that says if ($found) {, which I don't see aywhere in the code you posted. (If I'm wrong here, somebody let me know!). Maybe if you posted the code that contains that line?

        Guildenstern
        Negaterd character class uber alles!
      You can ignore the comments in the above code about container/containeeIndex being references to code - I changed it since then
RE (tilly) 1: Array mysteriously disappears on loop exit...
by tilly (Archbishop) on Oct 09, 2000 at 22:49 UTC
    The my declaration is lexical, so when you return from the function if that code is in another file you could be leaving the scope of that array. Declaring with use vars should solve that.

    But that said, using global like this is usually an indicator of poorly factored design. Instead design your code so that you return the array instead of manipulate it. That kind of design tends to be more flexible and ages much better.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://35911]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-19 09:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found