my @ComplicationsSurgicalProcedMedCare_238 = qw(27661 27783 27788 2853 28741); my @SuperficialInjuryContusion_239 = qw(9062 9063 9100 9101); my %diags = ( "Complications of surgical procedures or medical care" =>\@ComplicationsSurgicalProcedMedCare_238, "Superficial injury; contusion" =>\@SuperficialInjuryContusion_239, ); my %Diag; my $file = "2009.txt"; open my $fh, "<", $file or die "Can't open $file: $!"; while (<$fh>) { my ($RID, $DiagCode) = split; push @{ $Diag{$RID} }, $DiagCode; } #Print items of array to text file open my $fh1, '>', "Output by RID.txt" or die "Cannot open output.txt: $!"; foreach (my $RID) { print $fh1 Dumper(\%Diag); } my @Search = <$fh1> ; my %frequency; for my $Search (@Search) { for my $diag ( keys %diags ) { if( my @found = grep { $_ eq $Search } @{$diags{$diag}} ) { my $found = join ",", @found; print "Diagnosis Found: $diag, $found\n"; $frequency{$Search}++; } } } foreach (@Search) { print $fh1 "$_\n"; # Print each entry in our array to the file } close $fh1; #count how many times a variable is repeated my %counts = (); for (@Search) { $counts{$_}++; } print "\n Completed!\n";