relientmark has asked for the wisdom of the Perl Monks concerning the following question:
I understand the foreach goes through both arrays, and adds a count of each unique object in both arrays, could someone elaborate on how this works and what "$count{$element}++" means? Also the line "push @{ $count{$element} > 1 ? \@intersection : \@difference }, $element;" It appears to say if the value count it greater than one, but in intersection else put in difference? Although I dont understand why this work. where could I get some more information? Thanks in advance. Mark@union = @intersection = @difference = (); %count = (); foreach $element (@array1, @array2) { $count{$element}++ } foreach $element (keys %count) { push @union, $element; push @{ $count{$element} > 1 ? \@intersection : \@difference }, $eleme +nt; } "foreach $element (@array1, @array2) { $count{$element}++ }"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Clarification of code - find array intersection
by wind (Priest) on Mar 16, 2011 at 01:44 UTC | |
by relientmark (Novice) on Mar 16, 2011 at 03:18 UTC | |
by Anonymous Monk on Mar 16, 2011 at 17:24 UTC | |
|
Re: Clarification of code - find array intersection
by Marshall (Canon) on Mar 16, 2011 at 04:43 UTC | |
by relientmark (Novice) on Mar 21, 2011 at 10:07 UTC |