in reply to Referencing in Function not working correctly.

My referencing for @bonly is getting screwed up.

How is it "getting screwed up"?    What is it doing that it is not supposed to be doing?    Or what is it not doing that it is supposed to be doing?

my $bonly_ref = shift; # input array Bonly my @my_bonly = @$bonly_ref; # input array Bonly ... @my_bonly = (); # create null arrays

Why bother assigning anything to @my_bonly if you are just going to clear it out anyway?

my (%count, @isec, @diff, @union, @aonly) = ();

What do you think that the  = () at the end of that statment is doing?    What did you intend for it to do?

Replies are listed 'Best First'.
Re^2: Referencing in Function not working correctly.
by mr_p (Scribe) on Apr 02, 2010 at 15:48 UTC
    I am using @bonly where function is called from. I need the reference of the array. I didn't want to perl to copy onto another array.
      I am using @bonly where function is called from.

      Yes, I can see that.

      I need the reference of the array.

      Yes, that is obvious from the code.

      I didn't want to perl to copy onto another array.

      perl doesn't do any copying.    You however seem to like to copy all your arrays to new arrays.    What is that all about?

      Please explain why you are doing these things.

        "Please explain why you are doing these things."

        I thought it would be safer to copy it in the functions. So, what your saying is if I returned @bonly (as below), perl will not copy it will just give me pointer:
        @bonly = deDuppedArray(\@currentListing, \@previousListing, \@isec_plu +s_bonly );
        My original problem is @bonly changes after the function call and before function ended? Do you know why?
Re^2: Referencing in Function not working correctly.
by mr_p (Scribe) on Apr 02, 2010 at 18:36 UTC
    my (%count, @isec, @diff, @union, @aonly) = ();
    It is Initializing.

      The assignment does nothing. Freshly-declared aggregates are empty regardless of any useless assignments.

      What do you think it is initializing?

        OK, I got a working version. I things around as you suggested (hopefully). Now I want to make sure it is as efficient as possible. Can you please look at this code and see if it can get anymore efficient. or should I open a new thread?
        sub deDuppedArray { my ($a_ref, $b_ref, $my_bonly, $my_isec_plus_bonly) = @_; # refere +nce my (%Aseen, %Bseen, %count, @isec, @diff, @union, @aonly) = (); @Aseen{@$a_ref} = (); # lookup table @Bseen{@$b_ref} = (); # lookup table foreach my $e (@$a_ref, @$b_ref) { $count{$e}++ } # put all items +in hash table foreach my $e (keys %count) { # interate over each key of hash table push(@union, $e); # keys of hash table = union if ($count{$e} == 2) { push @isec, $e; # seen more than once = intersection } else { push(@aonly, $e) unless exists $Bseen{$e}; # seen once + f +rom A = Aonly push(@$my_bonly, $e) unless exists $Aseen{$e}; # seen once + + from A = Aonly } } @$my_isec_plus_bonly = ( @isec, @$my_bonly); }
        my @x = ("z", "y", "z"); @x = ();

        Doesn't the @x get initialize here? If I want to reuse @x what do I do to initialize, is there another way?