Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

by satans-nightmare (Initiate)
on Feb 22, 2013 at 09:01 UTC ( [id://1020109]=perlquestion: print w/replies, xml ) Need Help??

satans-nightmare has asked for the wisdom of the Perl Monks concerning the following question:

Hi All. I need to try and figure this out. I am searching an array for exact mathes in another array. If found it must tell me and also tell me if not found. I wrote this script and it works, but the issue is it does a search for each seperate string, return with a match or not and follow through, so each I will end up with lines not matching, and matching. The script.
@array = ( "54321" , "54312" , "5999" , "54352" , "12345" ); @original = ( "12345" , "54321" , "12355" ); foreach $string(@array) { foreach $string2(@original) { if ($string eq $string2) { $found = 1; } else { $found = 0; } if ($found == 0) { print "$string not found\n"; } else { print "$string found\n"; } } }
The result. 54321 not found 54321 found 54321 not found 54312 not found 54312 not found 54312 not found 5999 not found 5999 not found 5999 not found 54352 not found 54352 not found 54352 not found 12345 found 12345 not found 12345 not found Result I want though is non repeated search results. like this. 12345 Found 54321 Found 12355 Not found 54312 Not found 5999 Not found 54352 Not found I know it seems like I am wanting to do to much as I can atleast see what is found and what not, but this script is just a small variant of it's big brother, the original script needs to read thousands of lines and give me comparisons. Much appreciated in advance!

Replies are listed 'Best First'.
Re: Search in array from match in another array, print once only.
by vinoth.ree (Monsignor) on Feb 22, 2013 at 09:16 UTC

    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;
      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.
Re: Search in array from match in another array, print once only.
by tmharish (Friar) on Feb 22, 2013 at 09:11 UTC

    Change loop to:

    foreach $string(@array) { my $found = 0 ; foreach $string2(@original) { if ($string eq $string2) { $found = 1; } } if ($found == 0) { print "$string not found\n"; } else { print "$string found\n"; } }

    Also use strict and warnings ...

      More importantly its more efficient if you use a hash:

      use strict ; use warnings ; my @array = ( "54321", "54312", "5999", "54352", "12345" ) ; my @original = ( "12345" , "54321" , "12355" ) ; my %hash = map( { ( $_ => 1 ) } @original ) ; print "". ( ( $hash{ $_ } ) ? "$_ Found\n" : "$_ Not Found\n" ) foreac +h ( @array ) ;
Re: Search in array from match in another array, print once only.
by BillKSmith (Monsignor) on Feb 22, 2013 at 13:49 UTC

    Checkk the FAQ. Refer: perldoc -q intersection for the use of a hash in comparing arrays.

    Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1020109]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2025-03-20 18:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (61 votes). Check out past polls.