abhiram10 has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
I have a set of data already in a hash and I have an array separated with a ":" and a tab space. I want to add this array as a hash to my master hash, whose key is an element from the array. Here is what I have tried:
use strict; use warnings; my %MasterHash; my $line = "k1:v1 k2:v2 k3:v3"; #the space is a tab ##some part of code that writes stuff to MasterHash .... my @array1 = split (/\t/,$line); my ( $index )= grep { $array1[$_] =~ /v3/ } 0..$#array1; my $mv = splice @array1, $index,1; print "after removing: $mv\n"; my ($mvl,$mvr) = split (/:/,$mv,2); $MasterHash{$mvr} = map { split /:/; } @array1; print Dumper \%MasterHash;
What output I get:
$Var1 = { 'abc' => 'loaded before', 'xyz' => 'loaded before', 'v3' => 16 #basically some number };
What I expected:
$Var1 = { 'abc' => 'loaded before', 'xyz' => 'loaded before', 'v3' => { 'k1' => 'v1', 'k2' => 'v2' } };
Can anyone help me understand what I did wrong?
Also, if there is a cleaner way to achieve this goal of mine, please let me know, Would love to learn and it will be greatly appreciated!
Thanks in Advance
Abhi
|
|---|