Now i am modifying the code a little
I am using the code for creating hash haivng duplicate keys. Its giving the syntax error.
use strict;
use warnings;
my $s = "12 A P1
23 B P5
24 C P2
15 D P1
06 E P5";
my $hash;
my @a = split(/\n/, $s);
foreach (@a)
{
my $c = (split)[2];
my $d = (split)[1];
my $e = (split)[0];
push(@{$hash->{$c}}, $d);
}
print Dumper($hash );
i am getting the output as
$VAR1 = {
'P5' => [
'B',
'E'
],
'P2' => [
'C'
],
'P1' => [
'A',
'D'
]
};
But i want the output like
$VAR1 = {
'P5' => {
'E' => '06',
'B' => '23'
},
'P2' => {
'C' => '24'
},
'P1' => {
'A' => '12',
'D' => '15'
}
};
How to do that
Perl is telling you exactly what is wrong. You have used the
strictpragma, so using the%hashvariable without declaring it is a syntax error. While the string%hashdoes not appear in your code, the string$hash{...}does, on each of the problem lines. This is the syntax to access an element of the%hash, which is why strict is complaining.You have declared the variable
$hash, so accessing an element of the contained hash reference is written$$hash{...}or$hash->{...}. Fix the problem lines to access the correct variable and the code will compile.