My advice is to set up your data structures so as to make data access as simple and straightforward as possible. One of Perl’s greatest strengths is its highly-efficient built-in hashes, so use them to construct lookup tables for the required data. Here is how I would go about this:

#! perl use strict; use warnings; my @ComplicationsSurgicalProcedMedCare_238 = (27661, 27783, 27788, 28 +53, 28741); my @SuperficialInjuryContusion_239 = ( 9062, 9063, 9100, 91 +01); my ($infile, $outfile) = ('2009.txt', 'Output by +RID.txt'); my %DiagNames = ( 238 => 'Complications of surgical procedures or medical care', 239 => 'Superficial injury; contusion', ); my %DiagCodes; $DiagCodes{$_} = 238 for @ComplicationsSurgicalProcedMedCare_238; $DiagCodes{$_} = 239 for @SuperficialInjuryContusion_239; my %Diags; open my $fh, '<', $infile or die "Can't open file '$infile' for reading: $!"; while (<$fh>) { my ($RID, $DiagCode) = split; ++$Diags{$RID}{$DiagCode}; } close $fh or die "Can't close file '$infile': $!"; open my $fh1, '>', $outfile or die "Cannot open file '$outfile' for writing: $!"; for my $key (sort keys %Diags) { print $fh1 "$key\n{\n"; for (sort { $a <=> $b } keys %{ $Diags{$key} } ) { if (exists $DiagCodes{$_}) { printf $fh1 " %s: %d, %d\n", $DiagNames{ $DiagCodes{$_} }, $_, $Diags{$key}->{$_}; } else { printf $fh1 " Unrecognized code: %d\n", $_; } } print $fh1 "}\n\n"; } close $fh1 or die "Can't close file '$outfile': $!";

Input file “2009.txt”:

Tom_Jones 9062 John_Smith 27783 Tom_Jones 9062 Jane_Brown 9100 Tom_Jones 28741 John_Smith 9062 Tom_Jones 9062 Jane_Brown 9062 Tom_Jones 28741 Jane_Brown 2853 Jane_Brown 9062 Tom_Jones 12345

Output file “Output by RID.txt”:

Jane_Brown { Complications of surgical procedures or medical care: 2853, 1 Superficial injury; contusion: 9062, 2 Superficial injury; contusion: 9100, 1 } John_Smith { Superficial injury; contusion: 9062, 1 Complications of surgical procedures or medical care: 27783, 1 } Tom_Jones { Superficial injury; contusion: 9062, 3 Unrecognized code: 12345 Complications of surgical procedures or medical care: 28741, 2 }

P.S. In future, please supply actual sample data (not a description of what the data looks like — an actual sample of the data) together with the actual output which should result from the given input data. This will make it much easier for the Monks to help you. See How do I post a question effectively?

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^3: Putting Hash values into an array by Athanasius
in thread Putting Hash values into an array by Raya4505

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.