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"; } }
<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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |