in reply to Search multiple variables

If you have all diagnoses in arrays you could do the following:

use strict; use warnings; my %diags = ( "Meningitis" => \@array, "Encephalitis" => \@Encephalitis_77, # more of them here ); my $Search = qw( 6280 ); my $found_one = 0; for my $diag ( keys %diags ) { if( my @found = grep { $_ eq $Search } @{$diags{$diag}} ) { my $found = join ",", @found; print "Primary diagnosis: $diag, $found\n"; $found_one = 1; } } print "Sorry, \"$Search\" not found in diagnosis list\n" unless $found +_one;

Disclaimer: have not run this due to lack of sample data, so there might be errors in there. But the concept should be clear.

Update: changed @$diag to @{$diags{$diag}}.

Replies are listed 'Best First'.
Re^2: Search multiple variables
by Raya4505 (Novice) on Feb 20, 2014 at 20:53 UTC

    Ok, thank you so much for responding!!Your code works wonderfully. The only other thing is that I need to search multiple items (so instead of $Search, perahaps @Search or it could read from a formatted CSV file) and then I need perl to pick the one that shows up the most as the primary, but still display the other ones. If the results could display something like:

    Primary diagnosis (code that showed up the most for the particular per +son): Cancer of the throat (7) Other diagnosis: Benign Neoplasm (3 - as in it showed up three times) +Leukemias (2)

    I probably didn't make that very clear sorry, and I may be getting greedy here, but any help would be so appreciated!

    Do you have any ideas for that? THANK YOU

      Put another loop around the loop over the diagnoses:

      my @Search = qw( 6280 3141 2718 1414 ); my %frequency; for my $Search (@Search) { for my $diag ( keys %diags ) { if( my @found = grep { $_ eq $Search } @{$diags{$diag}} ) { my $found = join ",", @found; print "Primary diagnosis: $diag, $found\n"; $frequency{$Search}++; } } }

      and then do something based on the %frequency of found diagnoses.

        Did anyone every tell you that you are a genius? Because you definitely are. Thank you so much for helping me. thank you for everyone else who responded. I have used some of the other comments in other scripts of mine.