Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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 ( [id://1020112]=note: print w/replies, xml ) Need Help??


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

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)
  • Comment on Re^2: Search in array from match in another array, print once only.

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

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

    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.
      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!!

      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

Log In?
Username:
Password:

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

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










    Results (56 votes). Check out past polls.