Thanks to wind the code was formatted in a way that I could understand. Look at use of "code" tags in the formatting tips...

I have re-written this a bit...see below.

$count{$element}++; adds 1 to the $element entry in the %count hash table. If no such entry exists at the moment, Perl creates it for you. This is called auto-vivification - the hash entry "springs into existence" with a value of 0 and it gets incremented by one.

The other statement push @{ $count{$element} > 1 ? \@intersection : \@difference }, $element; is a rather complicated way of expressing an "if,else" statement. You can read about the ternary operator. Past that, this statement uses some tricky reference syntax that is not needed here. Do not make the mistake of assuming that fewer lines of source code means a faster program. My reformulated version will run just as fast and maybe even faster and I am sure that it will be easier for you to understand.

Update:
More typical code with ternary operator would be something like this: my $result = ($x > $y) ? 1: 0; This is an "if..else" statement.

(keys %count) does not have any particular order. That is the disadvantage of a hash table - you don't get easy sequential ordered access. Perl has some very good sorting functions so that you can get the order to come out like you want, but adding that to this code seemed an over complication given that we are still working on the basics.

#!/usr/bin/perl -w use strict; # this initialization to () is not necesssary, but ok my (@union, @intersection, @difference, %count) = (); my @array1 = (1,2,3); my @array2 = (3,4,1); foreach my $element (@array1, @array2) { $count{$element}++; #auto-vivification of a hash entry } foreach my $element (keys %count) # a print to show results so far { print "$element occured $count{$element} times\n"; } @union = keys %count; # same as the push @union, $element statement # it would be faster to have this within the # foreach loop, because calculating the keys # takes some "work", but I'm showing here # what this does. @union and keys %count are # equivalent. foreach my $element (keys %count) { if ($count{$element} > 1) #element occured more than once { push @intersection, $element; } else #element only occured once { push @difference, $element; } } print "union=@union; intersection=@intersection; difference=@differenc +e\n"; __END__ 4 occured 1 times 1 occured 2 times 3 occured 2 times 2 occured 1 times union=4 1 3 2; intersection=1 3; difference=4 2

In reply to Re: Clarification of code - find array intersection by Marshall
in thread Clarification of code - find array intersection by relientmark

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.