in reply to Hash::Merge
I was ending up with$a1 = [undef, item2]; $a2 = [item1, undef];
When I wanted$a3 = [item1, undef];
I've written an update that merges the arrays if items are undef like the example above. So far I've only tested with LEFT_PRECEDENT, but I don't see why it wouldn't work well for the others.$a3 = [item1, item2];
New sub:-'LEFT_PRECEDENT' => { 'SCALAR' => { 'SCALAR' => sub { $_[0] }, 'ARRAY' => sub { $_[0] }, 'HASH' => sub { $_[0] }, }, 'ARRAY' => { 'SCALAR' => sub { [ @{$_[0]}, $_[1] ] }, 'ARRAY' => sub { [ @{$_[0]}, @{$_[1]} ] }, 'HASH' => sub { [ @{$_[0]}, values %{$_[1]} ] }, }, 'HASH' => { 'SCALAR' => sub { $_[0] }, 'ARRAY' => sub { $_[0] }, 'HASH' => sub { _merge_hashes( $_[0], $_[1] ) }, }, },
I'm guessing that this would make a good option for people. I wont implement it fully as it's your module after all. But if you do it implement it I'd greatly appreciate a mention, love reading my name on CPAN ;)sub _merge_arrays { my ( $left, $right ) = ( shift, shift ); if( ref $left ne 'ARRAY' || ref $right ne 'ARRAY' ) { carp 'Arguments for _merge_arrays must be array references'; return; } my $length = $#$left; $length = $#$right if ($#$right > $length); my @newarray; for (my $num = 0; $num <= $length; $num++) { if ( defined $left->[$num] && defined $right->[$num] ) { @newarray[ $num ] = merge ( $left->[ $num ], $right->[ $num] ); } elsif ( defined $left->[$num] && !(defined $right->[$num]) ) { @newarray[ $num ] = $left->[ $num ]; } elsif ( !(defined $left->[$num]) && defined $right->[$num] ) { @newarray[ $num ] = $right->[ $num ]; } else { $newarray[$num] = $clone_behavior ? _my_clone( $left->[$num] ) : $left->[$num]; } }#for return \@newarray; }
|
|---|