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

Hi monks, I pulled this code from perlfaq4, which gives us the union, intersection, and difference between two arrays. I understand everything up to that last line with the ? mark. Could anyone please explain how that last line works? Thnx...
@union = @intersection = @difference = (); %count = (); foreach $element (@array1, @array2) { $count{$element}++ } foreach $element (keys %count) { push @union, $element; push @{ $count{$element} > 1 ? \@intersection : \@difference }, $e +lement; }

Replies are listed 'Best First'.
Re: union, intersection, and difference
by particle (Vicar) on May 02, 2002 at 19:49 UTC
    ?: is the ternary operator.

    it's equivalent to

    push @{ if( $count{$element} > 1 ) { \@intersection; } else { \@difference; } }, $element;
    (though a little less verbose)

    ~Particle *accelerates*

Re: union, intersection, and difference
by thelenm (Vicar) on May 02, 2002 at 19:51 UTC
    Here is the code with some of my own comments. Hopefully it is helpful.
    # initialize all sets to be empty @union = @intersection = @difference = (); %count = (); # count the number of times each unique element appears # in both arrays foreach $element (@array1, @array2) { $count{$element}++ } # for each unique element, determine whether it belongs # to the union, intersection, or difference of the original arrays foreach $element (keys %count) { # since the union includes all elements present in either # array, every unique element should belong to the union push @union, $element; # if this element occurred appeared more than once # (i.e., in both arrays), then it belongs to the intersection. # if it occurred only once (i.e., in only one array), then it # belongs to the difference. perlfaq4 explains that this is # -symmetric difference-, like an XOR operation. push @{ $count{$element} > 1 ? \@intersection : \@difference }, $e +lement; }
    The ternary ?: operator is a shorthand for if/then/else, and is explained further in perlop. The last statement works because push requires an array, which we get by dereferencing a reference to an array (like this: @{ \@array }).

    It seems to me, though, that this code only works if the elements of each array are unique. If the same element appears twice in one array, it will become part of the intersection set, although it shouldn't. (Update: If I had read perlfaq4 carefully myself, I would have seen that this assumption is documented. oops :-)

Re: union, intersection, and difference
by lachoy (Parson) on May 02, 2002 at 21:37 UTC

    @{ ... } is a dereferencing operation. It's useful to note that the { ... } is just a normal code block and can contain any code you want, as long as it returns an array reference.

    For instance, you could call a subroutine which returns an array reference:

    foreach my $case ( $justice_wheels->spin() ) { my $verdict = Conviction::Felony->new( $case->defendant() ); push @{ $case->defendant()->get_felony_convictions() }, $verdict; push @{ $case->judge()->get_felony_convictions() }, $verdict; }

    Yep, definitely feeling silly today...

    Chris
    M-x auto-bs-mode

      Ahhh... pretty cool, I'll be using ? definitely more in the future. Thanks!
Re: union, intersection, and difference
by Mr. Muskrat (Canon) on May 02, 2002 at 20:11 UTC
    Look in perlop under the section titled Conditional Operator.

    Matthew Musgrove
    Who says that programmers can't work in the Marketing Department?
    Or is that who says that Marketing people can't program?