Ah, your programs are becoming complex enough that the realization that there are always 2 people who need to understand the code is starting to sink in. Those 2 people are: (a) you when you write it and (b) you when you have to extend it or fix at some time in the future! And if it is a useful program, one of these 2 things and probably both will happen!
Some simplifications to the code will be possible, based upon your first example, I recoded the first part with some straightforward techniques and with just borrowing pretty much your regex'es. Your actual record is more complex. However, I hope that the lesson about spacing and indenting is becoming more clear - it helps a LOT.
#!/usr/bin/perl -w
use strict;
my $patient_id; #patient record for the CSV file
my $nhs;
my $dob;
my $sex;
my $postcode;
my $total_patients=0;
while (<DATA>)
{
s/^\s*//; #remove leading space
s/\s*$//; #remove trailing space
$patient_id = $1 if m/^Patient ID .*?(\d+)$/;
$nhs = $1 if m/^NAD .*?(\d+)$/;
$dob = $1 if m/^Date Of Birth .*?([\/\d]+)$/;
$sex = $1 if m/^Sex .*?(\w+)$/;
#record ends with postal code -> output to the CSV file
if ( ($postcode )= m/^Postcode .*?(\w+)$/ )
{
print "$patient_id,$nhs,$postcode,$dob,$sex\n"; #CSV line
$total_patients++; # this is here instead of with $patient_id
# because it is here that we have an
# "official" patient record
# this starts variables "over again". A runtime warning will
# happen if for some reason one of these winds up still
# being undefined when the record is printed to the CSV file
#
# note: doing this for $postcode is not necessary, but I
# treat this variable same as the others because this lessens
# the chance of a mistake when modifying the program later.
$patient_id = $nhs = $dob = $sex = $postcode = undef;
+
}
}
print "Total Patients: $total_patients\n";
=prints
1234,5678,BH78,01/01/1965,yes
4321,999,XYZ98,10/10/2007,no
Total Patients: 2
=cut
__DATA__
Patient ID 1234
NAD 5678
Date Of Birth : 01/01/1965
Sex : yes
Postcode : BH78
Patient ID 4321
NAD 999
Date Of Birth : 10/10/2007
Sex : no
Postcode : XYZ98