metaperl has asked for the wisdom of the Perl Monks concerning the following question:

I have an array ref of hash refs:
[ { 'time_of_request' => '2009-03-12 16:07:13', 'runmode' => 'authen_dummy_redirect', 'people_id' => '6877' }, { 'time_of_request' => '2009-03-12 16:07:14', 'runmode' => 'affiliates', 'people_id' => '6877' }, { 'time_of_request' => '2009-03-12 16:24:25', 'runmode' => 'affiliates', 'people_id' => '6877' }, { 'time_of_request' => '2009-03-12 16:24:45', 'runmode' => 'agents', 'people_id' => '6877' } ];
That I want to reformat with an arbitrary key.. let's say 'people_id':
{ 6877 => [ { 'time_of_request' => '2009-03-12 16:07:14', 'runmode' => 'affiliates', }, { 'time_of_request' => '2009-03-12 16:24:25', 'runmode' => 'affiliates', }, { 'time_of_request' => '2009-03-12 16:24:45', 'runmode' => 'agents', } ] 4211 => [ ... ], }
I know that I could write something to do this, but was hoping for a CPAN goodie, the functional inverse of Data::Hash::Flatten.

Replies are listed 'Best First'.
Re: remapping an arrayref of hashrefs to a hashref of array refs
by ikegami (Patriarch) on Mar 12, 2009 at 20:56 UTC
    my $src = [ ... ]; my %dst; for (@$src) { my $id = delete( $_->{people_id} ); push @{ $dst{$id} }, $_; }
Re: remapping an arrayref of hashrefs to a hashref of array refs
by BrowserUk (Patriarch) on Mar 12, 2009 at 21:20 UTC

    If people would expend as much effort learning to use the language as the do trying to avoid using it, it would save them so much time:

    my $x = [ your input ]; my %y; push @{ $y{ delete $_->{ people_id } } }, $_ for @$x;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      me no reinvent wheels mon

        Using native Perl isn't "re-inventing wheels". Larry invented the wheels many years ago, your just using them.

        Any api you or anyone devised to perform that operation would of necessity be either:

        1. So specific to that operation, that it would have no other use than that single operation.

          It would have to be called something like:

          sub createHoAoHfromAoHbyIndexingByTheValueOfOneKey{ my( $AoHref, $key ) = @_; ### About 20 lines of parameter checking and error reporting/excep +tion ### handling here. Because unlike doing it in-line, the programmer + writing ### this function cannot know what the caller will pass him. my %HoAoH; push @{ $HoAoH{ delete $_->{ $key } } }, $_ for @$AoHref; return \%HoAoH; }

          And to put that on CPAN, you'd need to wrap it in a package and add tests and documentation and dependencies and all bullshit--and how many times would it ever get used?

          And all of that to replace one, simple, long existing, simple, intuitive wheel.

          All it requires is for you to read the docs and apply a little thought; rather than expecting someone else to do your thinking for you.

        2. The only alternative would be some very generic, nested structure restructuring tool.

          Something that could equally be used to do:createAoHoAfromHoHoAbyTheValueOfOneKeyFromTheHashAndTheValueOfTheThirdElementOfTheSecondArray()

          And any API to do that kind of manipulation would have to be incredibly complex. Because it would essentially have to re-create all Perl's hash and array manipulation possibilities, without the benefit of Perl's very clear, concise and orthogonal syntax.

          And the complexity of any such undertaking would simply be ridiculous to the point of absurdity.

        You're not trying to avoid "reinventing wheels", just avoid having to think!


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
      not only that, but getting a good module means it covers a lot of useful cases and might be useful in a lot of places.

      I bend over backwards to find something on CPAN before writing new code.

      All new code must be tested, documented thoroughly. The community aspect of CPAN leads to that being done for me automatically.

        There are the right and wrong kinds of laziness you know

Re: remapping an arrayref of hashrefs to a hashref of array refs
by ig (Vicar) on Mar 12, 2009 at 21:06 UTC

    Maybe you would like Data::Hash::Transform.

    You can find this and other modules by searching cpan for hash or whatever other keywords might be relevant.