in reply to Re: Appending values to existing hash key
in thread Appending values to existing hash key

Thank you for replying so fast. Yes, doing a hash of arrays did occur to me. Something along the lines -

chomp; (my $k, my @v) = /(\S*\s*\S*\s*)(\S*\s*\S*\s*)/; push @{$h{$k}} , shift @v;

But dropped the idea because I had to iterate twice:

foreach my $key (sort keys %h) { print "$key"; foreach my $a (@{$h{$key}}) { print "$a "; } print "\n"; }

But I see how in a bigger data set this should be the way to go. Checking for existence in a larger hash could be counter productive.

Replies are listed 'Best First'.
Re^3: Appending values to existing hash key
by 2teez (Vicar) on Mar 21, 2014 at 04:11 UTC

    Hi jaypal,
    i totally agreed with ++NetWallah and really you can use a map for the nested for loop like so:

    use warnings; use strict; my %hash; while (<DATA>) { my @data = split; push @{ $hash{ $data[0] }{ $data[1] } } => "$data[2]: $data[3]"; } for my $key ( sort keys %hash ) { print $key, ' ', join( ', ' => map { $_, @{ $hash{$key}{$_} } } keys %{ $hash{$ke +y} } ), $/; } __DATA__ id1 name1 cat1 catname1 id1 name1 cat2 catname2 id2 name2 cat3 catname3 id3 name3 cat1 catname1 id3 name3 cat4 catname4


    UPDATE: Code updated as indicated by NetWallah.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
      The statement :
      join( ', ' => map { $_, @{ $hash{$key}{$_} } } keys $hash{$key} ), +$/;
      should read:
      join( ', ' => map { $_, @{ $hash{$key}{$_} } } keys %{ $hash{$key} +} ), $/;
      Nice shortcut (++) - nesting the hashes !

              What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                    -Larry Wall, 1992

        Thanks NetWallah for the extra eye! Staying up too late with C is not helping! And so do perl version 5.16.1! Am downgrading to v5.8.1! Lol :)

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me

      Wow, you guys are totally awesome. My first post (albeit lame) has got me so many great tricks and tips. Reading the map function initially from the camel book was a little overwhelming for me. But your usage in an example below has started my brain to make some sense out of it. Another valuable and code shortening gem you have offered is

      push @{ $hash{ $data[0] }{ $data[1] } } => "$data[2]: $data[3]";
      Thank you so much!
Re^3: Appending values to existing hash key
by Laurent_R (Canon) on Mar 21, 2014 at 07:24 UTC

    But I see how in a bigger data set this should be the way to go. Checking for existence in a larger hash could be counter productive.

    You probably don't have to worry about that, searches in hashes are fast and usually do not depend on the size of the hashes.
Re^3: Appending values to existing hash key
by hazylife (Monk) on Mar 21, 2014 at 10:41 UTC