Hello Raya4505, you wrote:
your line "if (exists my $DiagCodes{$_})"
But my line is:
if (exists $DiagCodes{$_})
— without the my! When you use my, you declare a new lexical variable. (And if the variable is not explicitly initialised in the declaration, it receives Perl’s default initialisation: undef if it’s a scalar, the empty list if it’s an array or hash.) So, remove the my and it should work correctly.
Here’s a useful reference: Coping with Scoping by Dominus.
Hope that helps,
| [reply] [d/l] [select] |
First of all, you are amazing. Thank you for your help. One thing I didn't put in my example and should have, is some of the diagnosis codes in my arrays start with a letter (e.g., V21564, c5415). So, I am struggling with your lines that say:
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", $_;
}
}
because they are set up for a numeric value with the <=>, so I changed it to:
foreach (sort {$a cmp $b} keys %{$Diags}$key}})
I tried some other things, but that is the one with the least errors, although it is still not running through. I PROMISE this will be the last question I ask for a long time!! Any insight you can give would be amazing!
| [reply] [d/l] [select] |
Tom_Jones z9062
John_Smith V27783
Tom_Jones z9062
Jane_Brown 9100
Tom_Jones 28741
John_Smith z9062
Tom_Jones z9062
Jane_Brown z9062
Tom_Jones 28741
Jane_Brown c2853
Jane_Brown z9062
Tom_Jones 12345
Next, the script itself. You’ve already handled the most challenging part by changing the sort operator from <=> to cmp. The only other changes required are to the array initialisations and the printf formats:
-
For the array initialisations, you need to quote the strings (because barewords are not allowed under strict 'subs'):
my @ComplicationsSurgicalProcedMedCare_238 = ( 27661, 'V27783', 27788,
+ 'c2853', 28741);
my @SuperficialInjuryContusion_239 = ('z9062', 9063, 9100,
+ 9101);
Or, you can make use of Perl’s qw operator (on which, see: Quote Like Operators):
my @ComplicationsSurgicalProcedMedCare_238 = qw(27661 V27783 27788 c28
+53 28741);
my @SuperficialInjuryContusion_239 = qw(z9062 9063 9100 91
+01);
-
The printf function format codes are as in C, and are detailed in the documentation for sprintf. %d formats its input as an integer, %s formats its input as a string. So this section of code:
if (exists $DiagCodes{$_})
{
printf $fh1 " %s: %d, %d\n",
# *** Change here: ^
$DiagNames{ $DiagCodes{$_} },
$_,
$Diags{$key}->{$_};
}
else
{
printf $fh1 " Unrecognized code: %d\n", $_;
# *** Change here: ^
}
needs to be changed to:
if (exists $DiagCodes{$_})
{
printf $fh1 " %s: %s, %d\n",
$DiagNames{ $DiagCodes{$_} },
$_,
$Diags{$key}->{$_};
}
else
{
printf $fh1 " Unrecognized code: %s\n", $_;
}
Hope that helps,
| [reply] [d/l] [select] |