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

Hello Monks,

I have a function that Compares 2 arrays and updates intersections of arrays and updates seconds arary only elements. My referencing for @bonly is getting screwed up. Can some one tell me why it is the case?

# Calling deDuppedArray(\@currentListing, \@previousListing, \@bonly, \@isec_plu +s_bonly);
sub deDuppedArray { my $a_ref = shift; # reference to input array A my @a = @$a_ref; # input array A my $b_ref = shift; # reference to input array B my @b = @$b_ref; # input array B my $bonly_ref = shift; # input array Bonly my @my_bonly = @$bonly_ref; # input array Bonly my $isec_plus_bonly_ref = shift; # input array isec_plus_bonly my @my_isec_plus_bonly = @$isec_plus_bonly_ref; # input array isec +_plus_bonly my (%Aseen, %Bseen) = (); @Aseen{@a} = (); # lookup table @Bseen{@b} = (); # lookup table my (%count, @isec, @diff, @union, @aonly) = (); @my_bonly = (); # create null arrays foreach my $e (@a, @b) { $count{$e}++ } # put all items in hash ta +ble 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 @diff, $e; # seen once = difference 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); }

Replies are listed 'Best First'.
Re: Referencing in Function not working correctly.
by jwkrahn (Abbot) on Apr 02, 2010 at 15:29 UTC
    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?

      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.

      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?

Re: Referencing in Function not working correctly.
by FunkyMonk (Chancellor) on Apr 02, 2010 at 15:27 UTC
    This

    My referencing for @bonly is getting screwed up
    is next to useless as a description of whatever the error you're seeing. However, shouldn't you be returning your modified arrays (as arrayrefs), Or are you trying to pass the arrays by reference? If that's the case leave them as arrayrefs throughout your subroutine.

    Incidently, you can write

    my $a_ref = shift; # reference to input array A my @a = @$a_ref; # input array A

    as

    my @a = @{+shift};

    and you don't need to initialise arrays and hashed when you declare them. They start out empty.

    Update: s/shift/+shift/