in reply to Need help to make correction in a perl script

How about this? (works for me with test data)

use strict; use warnings; my %code_to_full; my %field_by_code; open my $f, '<', 'test.txt' or die "open: $!"; my $last_code; while (<$f>) { my ($code, $full) = /^([A-Z]{2}):\s+(.+)/i; if ($code && $full) { $code_to_full{$code} = $full; $field_by_code{$code} = ''; $last_code = $code; } else { unless ($last_code) { warn "No code and last_code blank; this shouldn't happen"; } $field_by_code{$last_code} .= $_; } } for (sort keys %code_to_full) { print "Code: $_\n"; print "Full: $code_to_full{$_}\n"; print "Field data: $field_by_code{$_}\n\n"; }

Things I'd like to point out:


"Half of all adults in the United States say they have registered as an organ donor, although only some have purchased a motorcycle to show that they're really serious about it."