You could do something like:
use strict;
use warnings;
use 5.010;
use Data::Dumper;
my $hash;
my $year;
my $state;
while(<DATA>) {
chomp;
if(/^Year:(\d+)/) {
$year = $1;
$hash->{$year} = {};
next;
} elsif (/^State:/) {
$state = $_;
$hash->{$year}{$state} = [];
next;
} elsif(/^District/) {
push @{$hash->{$year}{$state}}, $_;
}
}
say Dumper$hash;
__DATA__
Year:2001
State: A
District A
District B
State: B
District A
District B
Year:2002
State: A
District A
District B
State: B
District A
District B
Year:2012
State: A
District A
District B
State: B
District A
District B
output:
$VAR1 = {
'2002' => {
'State: B' => [
'District A',
'District B'
],
'State: A' => [
'District A',
'District B'
]
},
'2001' => {
'State: B' => [
'District A',
'District B'
],
'State: A' => [
'District A ',
'District B'
]
},
'2012' => {
'State: B' => [
'District A',
'District B'
],
'State: A' => [
'District A ',
'District B'
]
}
};