Yes, this is not clear. If you have two arrays of numbers, and you are checking whether the numbers in array1 are present in array2, then you just store the array2 numbers as keys of a hash, then iterate over array1 and check whether each value exists as a hash key.

If some values are repeated in array2, the hash will have only the unique values as keys (there will be fewer hash keys than there are array elements). But why would that be a problem? In your comment about what you've "been thinking of" (making a hash of arrays), you mention some sort of "information" associated with each number, but what is this "information" and where does it come from? The array is just a list of numbers (some of which might appear more than once).

If it's important to know how many times a given value occurs in array2, just load and use the hash like this:

my %a2hash; $a2hash{$_}++ for ( @array2 ); for ( @array1 ) { if ( exists( $a2hash{$_} )) { # use the value that matched, and number of matches in @array2 +: do_something( $_, $a2hash{$_} ); } }
And of course, for any value that is repeated in array1 and happens to match a value found in array2, "do_something()" will happen each time that value comes up.

If the question is simply "which of the values in array1 are also found in array2?", then a simple hash with array2 values as keys is all you need, regardless of whether the values are repeated.

Did you maybe leave out some other aspect of the question?


In reply to Re: finding exists on non unique information by graff
in thread finding exists on non unique information by Anonymous Monk

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.