H
Henry Law
I wonder if someone can help me with working out where to define my
variables so as to give the desired result here...
Personnel data is to be read from a file. Attributes for each
employee are presented as name/value pairs; some pairs occur once
only, but each person can have more than one qualification. The data
is to be accumulated in a hash, keyed by the person's name; each
person's hash contains their attributes, plus an reference to an array
containing their qualifications. This enables me (using other
programming not relevant here) to look up any employee by name and
present their attributes and qualifications.
Here's a runnable program which shows where I've got to. It gives the
wrong results and I'm trying to work out how to fix it.
--------------- <start of example> -----------------
#! C:\Perl\bin\perl.exe
use strict;
use warnings;
use Data:umper;
my %people;
my %attrs;
my @quals;
while (my $rec = <DATA>) {
#my %attrs;
#my @quals;
chomp $rec;
if ($rec) {
my ($name,$value) = split /:/, $rec;
if ($name eq "qual") {
push @quals,$value;
next;
}
$attrs{$name} = $value;
} else { # End of person
$attrs{quals} = \@quals;
$people{$attrs{name}} = \%attrs;
#undef %attrs;
#undef @quals;
}
}
print Dumper(%people);
__END__
name:alice
occupation:analyst
born:aberdeen
qual:a1
qual:a2
name:bob
occupation:baker
status:broken
qual:b1
qual:b2
---------------- <end of example> ------------------
This results in
$VAR1 = 'alice';
$VAR2 = {
'quals' => [
'a1',
'a2',
'b1',
'b2'
],
'status' => 'broken',
'name' => 'bob',
'born' => 'aberdeen',
'occupation' => 'baker'
};
I can see what's wrong: %attrs and @quals have scope outside the
file-read loop, so when the program gets to Bob's records they either
add to or replace Alice's. But I can't define them inside the read
loop because they then disappear and reappear with each record (you
can see where I tried this in the comments). I've also tried
undef-ing the arrays at the end of each person (also as shown),
without getting the desired result.
Can someone make some suggestions? I'm prepared to structure the
whole program differently but the data format is a given.
variables so as to give the desired result here...
Personnel data is to be read from a file. Attributes for each
employee are presented as name/value pairs; some pairs occur once
only, but each person can have more than one qualification. The data
is to be accumulated in a hash, keyed by the person's name; each
person's hash contains their attributes, plus an reference to an array
containing their qualifications. This enables me (using other
programming not relevant here) to look up any employee by name and
present their attributes and qualifications.
Here's a runnable program which shows where I've got to. It gives the
wrong results and I'm trying to work out how to fix it.
--------------- <start of example> -----------------
#! C:\Perl\bin\perl.exe
use strict;
use warnings;
use Data:umper;
my %people;
my %attrs;
my @quals;
while (my $rec = <DATA>) {
#my %attrs;
#my @quals;
chomp $rec;
if ($rec) {
my ($name,$value) = split /:/, $rec;
if ($name eq "qual") {
push @quals,$value;
next;
}
$attrs{$name} = $value;
} else { # End of person
$attrs{quals} = \@quals;
$people{$attrs{name}} = \%attrs;
#undef %attrs;
#undef @quals;
}
}
print Dumper(%people);
__END__
name:alice
occupation:analyst
born:aberdeen
qual:a1
qual:a2
name:bob
occupation:baker
status:broken
qual:b1
qual:b2
---------------- <end of example> ------------------
This results in
$VAR1 = 'alice';
$VAR2 = {
'quals' => [
'a1',
'a2',
'b1',
'b2'
],
'status' => 'broken',
'name' => 'bob',
'born' => 'aberdeen',
'occupation' => 'baker'
};
I can see what's wrong: %attrs and @quals have scope outside the
file-read loop, so when the program gets to Bob's records they either
add to or replace Alice's. But I can't define them inside the read
loop because they then disappear and reappear with each record (you
can see where I tried this in the comments). I've also tried
undef-ing the arrays at the end of each person (also as shown),
without getting the desired result.
Can someone make some suggestions? I'm prepared to structure the
whole program differently but the data format is a given.