Reputation: 716
I have a file which contains states in the format such as Gujarat, West Bengal, Jammu & Kashmir and D&D Haveli. I have written a regex to get such names. These names go into the key of a 'year'(2001) such that it is also obtained from the regex such that the year is the key along with various states being the part of the same key.
my $stat;
my ($line, $year, $state_name, @state_name);
while($line = <FH>){
if($line =~ m/^Year (\d+)\S+/){
$year = $1;
$stat->{$year} = {};
next;
}
elsif ($line =~ m/^State:,(\w+\s\w+)/){
$state_name = $1;
$stat->{$year}{$state_name} = {};
next;
}
elsif ($line =~ m/^State:(\w+)/){
$state_name = $1;
$stat->{$year}{$state_name} = {};
next;
}
elsif ($line =~ m/^State:(\w&\w\s\w+)/){
$state_name = $1;
$stat->{$year}{$state_name} = {};
next;
}
elsif ($line =~ m/^State:(\w+\s&\s\w+)/){
$state_name = $1;
$stat->{$year}{$state_name} = {};
next;
}
}
print (Dumper(\$stat));
I want something now like this to be printed:
$VAR2 = {'2001' => {
'Gujarat'
'Jammu & Kashmir'
'West Bengal'
'D&D Haveli'
}
}
Instead only West Bengal is printed in the hash with key as 2001 and others are omitted. Please can you suggest where am I going wrong. Thank you.
The edited file is as follows:
Year 2001,,,,,,,,
State:,West Bengal,,,,,,,
Year 2001,,,,,,,,
State:,Gujarat,,,,,,,
Year 2001,,,,,,,,
State:,Jammu & Kashmir,,,,,,, and so on.
Upvotes: 0
Views: 86
Reputation: 5598
The code:
if($line =~ m/^Year (\d+)\S+/){
$year = $1;
$stat->{$year} = {};
next;
}
Will overwrite the structure under $stat->{$year}
if it exists, because your "2001" year value appears more than once
A quick fix:
if($line =~ m/^Year (\d+)\S+/){
$year = $1;
if (not defined $stat->{$year}) {
$stat->{$year} = {};
}
next;
}
Upvotes: 2