Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Search in array from match in another array, print once only.

by vinoth.ree (Parson)
on Feb 22, 2013 at 09:16 UTC ( #1020111=note: print w/ replies, xml ) Need Help??


in reply to Search in array from match in another array, print once only.

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;


Comment on Re: Search in array from match in another array, print once only.
Select or Download Code
Re^2: Search in array from match in another array, print once only.
by satans-nightmare (Initiate) on Feb 22, 2013 at 09:28 UTC
    Ah awesome, you guys rock! Don't know how I did not think of doing that. Thanks a million guys! Once last question. In some instances the file will have an entry twice in the original array .how can I match to print the string once but also print that I found it twice. for instance found 12345 (2 matches)

      Use the code below to say matched element count also, this will tell you the possible matches not only twice.

      use strict; use warnings; use Data::Dumper; my %hash_count; my @array = ( "54321", "54312" , "5999" , "54352" , "12345" + ); my @original = ( "12345" , "54321" , "12355", "54321" ); foreach my $string(@array) { foreach my $string2(@original) { if ($string eq $string2) { $hash_count{$string}++; } else { unless (exists $hash_count{$string}) { $hash_count{$string} = 0; } } } } print Dumper \%hash_count; foreach (keys %hash_count) { unless ($hash_count{$_}) { print "$_ Not found\n"; } else { print "$_ found $hash_count{$_} times\n" ; } }

      Track counts instead of assign 1 to hash:

      use strict ; use warnings ; my @array = ( "54321", "54312", "5999", "54352", "12345" ) ; my @original = ( "12345" , "54321" , "12345" ) ; my %count ; map( { $count{ $_ }++ } @original ); print "". ( ( $count{ $_ } ) ? "$_ Found $count{ $_ } times.\n" : "$_ +Not Found\n" ) foreach ( @array ) ;

      Output:

      54321 Found 1 times. 54312 Not Found 5999 Not Found 54352 Not Found 12345 Found 2 times.

        just try this one

        #!C:\Perl64\bin -w use strict; my @array = ( "54321" , "54312" , "5999" , "54352" , "12345" ); my @original = ( "12345" , "54321" , "12355" ); my $hash={}; foreach my $val(@original){ if (grep{$_ == $val} @array){ $hash->{$val}++; }##end if else { print "$val NOT MATCHED\n"; }##end else }##end foreach foreach (keys(%{$hash})){ print "$_ matched $hash->{$_} times\n"; }##end foreach
        Thanks a million guys!! Much appreciated! I can use each of these in different areas. Now just for me to go sit rewrite everything in my code to open the files and get arrays of the data. Again, thanks a mill for the help!!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1020111]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2014-10-01 22:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (40 votes), past polls