Hi, In your code if the first array element is matched with second array means you need to break the inner loop and need to process the next element from the first array.

The problem with your code is.

When the first array element is matched with the second array element you are setting the $found=1 and the first array element again checked with the other elements of second array, So if not matched $found=0 So when the $found is 1 printing matched, when it is 0 it prints not matched.

Try this code:

use strict; use warnings; use utf8; my $found; my @array = ("54321","54312","5999","54352","12345"); my @original =("12345","54321","12355"); foreach my $string(@array) { foreach my $string2(@original) { if ($string eq $string2) { $found = 1; last; } else { $found = 0; } } if ($found == 0) { print "$string not found\n"; } else { print "$string found\n"; } }

Update:

Use Array::Utils module from CPAN for comparing two arrays.
<p>Example:</p> use strict; use warnings; use Data::Dumper; use Array::Utils qw(:all); my @a = qw( a b c d ); my @b = qw( c d e f ); my @diff = array_diff(@a, @b); print "Difference:\n"; print Dumper \@diff; print "Intersection:\n"; my @isect = intersect(@a, @b); print Dumper \@isect; print "Unique union:\n"; my @unique = unique(@a, @b); print Dumper \@unique; # check if arrays contain same members if ( !array_diff(@a, @b) ) { # do something } # get items from array @a that are not in array @b my @minus = array_minus( @a, @b ); print Dumper \@minus;

In reply to Re: Search in array from match in another array, print once only. by vinoth.ree
in thread Search in array from match in another array, print once only. by satans-nightmare

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.