in reply to combine two AoHs
@c = map { +{%{$a[$_]}, %{$b[$_]}} } 0 .. @a - 1;
Update: it appears that I've interpreted your question differently from diotalevi. Here's an example on when they're different.
Output:#!/usr/bin/perl use strict; use Data::Dump::Streamer; my @a = ({ domain => 'adomain.com', data => 'adata', }, { domain => 'bdomain.com', data => 'bdata', }); my @b = ({ domain => 'bdomain.com', otherdata => 'bstuff', }, { domain => 'adomain.com', otherdata => 'astuff', }); my @c_ambrus = map { {%{$a[$_]}, %{$b[$_]}} } 0 .. @a - 1; sub merge_aoh_by_domain { my %merge; for my $domain_ref ( @_ ) { my $to_merge = $merge{$domain_ref->{domain}} ||= {}; @$to_merge{ keys %$domain_ref } = values %$domain_ref; } return values %merge; } my @c_diotalevi = merge_aoh_by_domain( @a, @b ); Dump(\@c_ambrus, \@c_diotalevi)->Names(q"@c_ambrus", q"@c_diotalevi")- +>Out; __END__
@c_ambrus = ( { data => 'adata', domain => 'bdomain.com', otherdata => 'bstuff' }, { data => 'bdata', domain => 'adomain.com', otherdata => 'astuff' } ); @c_diotalevi = ( { data => 'adata', domain => 'adomain.com', otherdata => 'astuff' }, { data => 'bdata', domain => 'bdomain.com', otherdata => 'bstuff' } );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: combine two AoHs
by alienhuman (Pilgrim) on Mar 07, 2006 at 01:18 UTC |