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

  • Comment on Adding an element of an array as a key for a Hash, and remaining elements as its value (would also be a hash)
  • Select or Download Code

Replies are listed 'Best First'.
Re: Adding an element of an array as a key for a Hash, and remaining elements as its value (would also be a hash)
by Anonymous Monk on Apr 12, 2016 at 02:04 UTC

    wild guess

    $MasterHash{$mvr} = { map { split /:/; } @array1 };

      Thank you Monk! That totally worked. I guess I should have tried the curly braces! Thank you very much for pointing out. This really helps!

      The next question I have is how did this work and not the one I tried ? Could you please explain to me? There is something that I have to learn about these braces

        ... something that I have to learn about these braces

            $MasterHash{$mvr} = { map { split /:/; } @array1 };
                                ^     ^            ^         ^
                                |     |            |         |
                                |     + sub block  +         |
                                |                            |
                                + anon hash ref constructor  +
        See perlref for anonymous reference constructors (in particular, Making References section 3 for the anon hash ref constructor) and also perlreftut. See map for the function of the sub(routine) block.


        Give a man a fish:  <%-{-{-{-<

        also map in scalar context produces a count of the number of elements instead of the list of elements

Re: Adding an element of an array as a key for a Hash, and remaining elements as its value (would also be a hash)
by Anonymous Monk on Apr 12, 2016 at 02:16 UTC
    #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; my $line = "k1:v1 k2:v2 k3:v3"; my %pairs; while( $line =~ m{\b([^:\s]+):([^:\s]+)\b}g ){ my( $k, $v ) = ( $1, $2 ); $pairs{$k}=$v; } dd( \%pairs ); if( delete $pairs{'k3'} ){ %pairs = ( k3 => { %pairs } ); } dd( \%pairs ); __END__ { k1 => "v1", k2 => "v2", k3 => "v3" } { k3 => { k1 => "v1", k2 => "v2" } }