Raya4505 has asked for the wisdom of the Perl Monks concerning the following question:

First, thanks in advance to anyone who takes the time to help me.

Second, I am self-taught, new to perl, so please try to make answers easy to read aka: simple.

Question: I am needing a script that will allow someone to enter multiple diagnosis codes (100+) in a text file or CSV file or array and then those diagnosis codes will match up with another list in a text file or CSV file or multiple arrays and if they match they will be renamed with their technical diagnosis name. This is what I have so far…

my $Search = qw(6280); if ( @found = grep { $_ eq $Search } @array) { my $found = join ",", @found; print "Primary Diagnosis: Meningitis, $found\n"; } if ( @found = grep { $_ eq $Search } @Encephalitis_77 ) { my $found = join ",", @found; print "Primary Diagnosis: Encephalitis, $found\n"; } else { print "Sorry, \"$Search\" not found in diagnosis list\n"};

But this only allows me to search one diagnosis code at a time which won’t work and it’s so much coding because I will have to do a grep function for each diagnosis and there are 1,000+. There must be a simpilar way!! Also, I am then needing the script to determine if one code was found more than any other code and label that as the patient’s primary diagnosis.

I have tried listing them in arrays, in hashes with arrays, in text files, in CSV files and unfortunately I may get it to find a couple codes, but I can’t figure out how to rename multiple codes and then also find the code that was matched the most. Hope this question is clear. Thank you for your help.

Replies are listed 'Best First'.
Re: Search multiple variables
by hdb (Monsignor) on Feb 20, 2014 at 15:37 UTC

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

      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.

Re: Search multiple variables
by ww (Archbishop) on Feb 20, 2014 at 15:18 UTC
    If you have access to a complete list of code and diagnoses, use them in a dispatch table.

    Super Search and or Google will help you learn about use and uses.

    Come, let us reason together: Spirit of the Monastery
Re: Search multiple variables
by kcott (Archbishop) on Feb 20, 2014 at 16:10 UTC

    G'day Raya4505,

    Welcome to the monastery.

    Firstly, I'm unsure on a couple of things so I've assumed: @array and @Encephalitis_77 are two different sources that you want to search; and, @found contains related information beyond the "Primary Diagnosis". You'll probably need to make changes to the example code below; however, the basic technique should be valid.

    If you organise your data into a hash with a structure like this:

    code1 => { source1 => { primary => 'diagnosis', other => 'information', }, source2 ... }, code2 ...

    You can write a short subroutine to quickly search for the information you want. The basic technique (with a couple of example searches) are shown in this script:

    #!/usr/bin/env perl -l use strict; use warnings; my %data_for = ( 6280 => { array => { primary => 'Meningitis', other => 'Other relevant info ...', }, Encephalitis_77 => { primary => 'Encephalitis', other => 'Other relevant info ...', }, }, ); search_for_diagnosis_code($_) for (6280, 9999); sub search_for_diagnosis_code { my ($search) = @_; print "Searching for '$search' ..."; if (exists $data_for{$search}) { for my $source (keys %{$data_for{$search}}) { print "\tFound in '$source':"; print "\t\tPrimary Diagnosis: $data_for{$search}{$source}{ +primary}"; print "\t\tOther Information: $data_for{$search}{$source}{ +other}"; } } else { print "\tNo data found."; } }

    Output:

    Searching for '6280' ... Found in 'array': Primary Diagnosis: Meningitis Other Information: Other relevant info ... Found in 'Encephalitis_77': Primary Diagnosis: Encephalitis Other Information: Other relevant info ... Searching for '9999' ... No data found.

    -- Ken

Re: Search multiple variables
by wjw (Priest) on Feb 20, 2014 at 19:27 UTC
    I think anonymous has it right. A database sounds like the way to go to me.

    An SQLite3 DB is a single file, easy to set up, and pretty darn fast for small data sets
    like you seem to be talking about. Once set up, you can use Perl to provide the front end
    for adding, deleting and searching for record matches. It is not that Perl is a
    poor solution for this, just not the best one from my point of view...

    Just a thought...and best of luck with whatever you do!

    ...the majority is always wrong, and always the last to know about it...
    Insanity: Doing the same thing over and over again and expecting different results.
Re: Search multiple variables
by Anonymous Monk on Feb 20, 2014 at 16:20 UTC
    Sure sounds like a database JOIN to me ... why write a custom program to do that?