udvk009 has asked for the wisdom of the Perl Monks concerning the following question:
Dear monks , can you throw some light on below issue as i am unable to figure out why my hash structure is messing up while referencing array as the key value. I had done the same is past for another script and conceptually it works but somehow in this case not sure whats wrong. My code is below. I have added all but just the main logic where hash is generated
my %parCode = ( 0 => "partition-1", 1 => "partition-2", 2 => "partition-3", 3 => "partition-4" ); while (my $in_line = <INFILE>) { @time_arr = (); chomp($in_line); if ($in_line =~ /Registered listener on template/){ #print "$in_line\n"; @row = split('\s',$in_line); ($r{time},$r{partition}) = ($row[0],$row[-1]); $r{partitionName} = $parCode{$r{partition}}; #print Dumper(\@row); #print Dumper(\%r); if (exists $parMap_ha{$r{partitionName}}){ #print " >> ### Time event detected - $r{partition} - $pa +rCode{$r{partition}}\n"; #print Dumper(\%parMap_ha); push(@time_arr,@{$parMap_ha{$r{partition +Name}}},$r{time}); #print Dumper(\@time_arr); $parMap_ha{$r{partitionName}} =\@time_ +arr; #print " << ### post modification \n"; #print Dumper(\%parMap_ha); } else{ $parMap_ha{$r{partitionName}} =[$r{tim +e}]; #print " >> *** 1st event detected - $r{partition} - $r{ +partitionName}\n"; #print Dumper(\%parMap_ha); } } }
Below is the hash dump with infile data where you can see that this works for the 1st few iterations and later the hash messes up
## ARRAY @row ### $VAR1 = [ '20160515-20:46:58.945+0900', 'server', 'id:', '2' ]; ## HASH %r ### $VAR1 = { 'time' => '20160515-20:46:58.945+0900', 'partitionName' => 'partition-3', 'partition' => '2' }; >> *** 1st event detected - 2 - partition-3 ## HASH %parMap_ha ### $VAR1 = { 'partition-3' => [ '20160515-20:46:58.945+0900' ] }; $VAR1 = [ '20160515-20:46:59.138+0900', 'server', 'id:', '3' ]; $VAR1 = { 'time' => '20160515-20:46:59.138+0900', 'partitionName' => 'partition-4', 'partition' => '3' }; >> *** 1st event detected - 3 - partition-4 $VAR1 = { 'partition-4' => [ '20160515-20:46:59.138+0900' ], 'partition-3' => [ '20160515-20:46:58.945+0900' ] }; $VAR1 = [ '20160515-20:46:59.145+0900', 'id:', '0' ]; $VAR1 = { 'time' => '20160515-20:46:59.145+0900', 'partitionName' => 'partition-1', 'partition' => '0' }; >> *** 1st event detected - 0 - partition-1 $VAR1 = { 'partition-4' => [ '20160515-20:46:59.138+0900' ], 'partition-1' => [ '20160515-20:46:59.145+0900' ], 'partition-3' => [ '20160515-20:46:58.945+0900' ] }; $VAR1 = [ '20160515-20:46:59.153+0900', 'id:', '1' ]; $VAR1 = { 'time' => '20160515-20:46:59.153+0900', 'partitionName' => 'partition-2', 'partition' => '1' }; >> *** 1st event detected - 1 - partition-2 $VAR1 = { 'partition-4' => [ '20160515-20:46:59.138+0900' ], 'partition-2' => [ '20160515-20:46:59.153+0900' ], 'partition-1' => [ '20160515-20:46:59.145+0900' ], 'partition-3' => [ '20160515-20:46:58.945+0900' ] }; $VAR1 = [ '20160516-01:04:40.210+0900', 'id:', '3' ]; $VAR1 = { 'time' => '20160516-01:04:40.210+0900', 'partitionName' => 'partition-4', 'partition' => '3' }; >> ### Time event detected - 3 - partition-4 ## HASH %parMap_ha ### $VAR1 = { 'partition-4' => [ '20160515-20:46:59.138+0900' ], 'partition-2' => [ '20160515-20:46:59.153+0900' ], 'partition-1' => [ '20160515-20:46:59.145+0900' ], 'partition-3' => [ '20160515-20:46:58.945+0900' ] }; ## ARRAY @time_arr ### $VAR1 = [ '20160515-20:46:59.138+0900', '20160516-01:04:40.210+0900' ]; << ### post modification ## HASH %parMap_ha ### $VAR1 = { 'partition-4' => [ '20160515-20:46:59.138+0900', '20160516-01:04:40.210+0900' ], 'partition-2' => [ '20160515-20:46:59.153+0900' ], 'partition-1' => [ '20160515-20:46:59.145+0900' ], 'partition-3' => [ '20160515-20:46:58.945+0900' ] }; $VAR1 = [ '20160516-01:04:57.643+0900', 'id:', '1' ]; $VAR1 = { 'time' => '20160516-01:04:57.643+0900', 'partitionName' => 'partition-2', 'partition' => '1' }; >> ### Time event detected - 1 - partition-2 ## HASH %parMap_ha ### ## LOOK at the hash value as its starting to messup ## $VAR1 = { 'partition-4' => [], 'partition-2' => [ '20160515-20:46:59.153+0900' ], 'partition-1' => [ '20160515-20:46:59.145+0900' ], 'partition-3' => [ '20160515-20:46:58.945+0900' ] }; $VAR1 = [ '20160515-20:46:59.153+0900', '20160516-01:04:57.643+0900' ]; << ### post modification ## LOOK at the hash value (line-2) as its starting to messup ## $VAR1 = { 'partition-4' => [ '20160515-20:46:59.153+0900', '20160516-01:04:57.643+0900' ], 'partition-2' => $VAR1->{'partition-4'}, 'partition-1' => [ '20160515-20:46:59.145+0900' ], 'partition-3' => [ '20160515-20:46:58.945+0900' ] };
As always thanks in advance!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem is assigning array as hash value
by Discipulus (Canon) on May 19, 2016 at 10:49 UTC | |
by udvk009 (Novice) on May 19, 2016 at 23:54 UTC |