That "last" made you miss entries. To make you realize your mistake, here is the code you can compare with: (But this is not the solution to go for speed. Say the two arrays are of size n and m. In the worst case, this solution costs n*m, whereas use a hash (for DB) will only cost the size of the input data)

use strict; use warnings 'all'; my @DB = qw(AB1/1 AB1/5 AB2/5); my @Input = qw(AB1/1 AB1/2 AB1/5 AB1/6 AB1/9 AB2/2 AB2/5 AB2/6 AB2/9 A +B2/13); for my $InputData (@Input) { my $found; for my $DBData (@DB) { if ($DBData eq $InputData) { $found = 1; last; } } print "$InputData : " . ($found?" ":" Not") . "Found\n"; }

Which prints:

AB1/1 : Found AB1/2 : NotFound AB1/5 : Found AB1/6 : NotFound AB1/9 : NotFound AB2/2 : NotFound AB2/5 : Found AB2/6 : NotFound AB2/9 : NotFound AB2/13 : NotFound

Update: removed the extra brace liverpole found. Copy and Paste... Thanks!


In reply to Re: searching 2 arrays by pg
in thread searching 2 arrays 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.