Merge hashes with arrays with Hash::Merge - perl

I am trying to merge two hashes which contains one or more arrays using Hash::Merge. For example:
use strict;
use warnings;
use feature qw(say);
use Data::Dump qw(dump);
use Hash::Merge qw(merge);
my $h1 = { a => [ { aa => 1 }, 3 ] };
my $h2 = { a => [ { bb => 2 } ] };
my $hLeft = merge( $h1, $h2 );
my $hRight = merge( $h2, $h1 );
say " hLeft: " . dump($hLeft);
say " hRight: " . dump($hRight);
my $hDesired = { a => [ { aa => 1, bb => 2 }, 3 ] };
say "Desired: " . dump($hDesired);
This gives output:
hLeft: { a => [{ aa => 1 }, 3, { bb => 2 }] }
hRight: { a => [{ bb => 2 }, { aa => 1 }, 3] }
Desired: { a => [{ aa => 1, bb => 2 }, 3] }
How can I get the correct output using Hash::Merge ?

This can be done using Hash::Merge::specify_behavior :
use warnings;
use strict;
use Data::Dump 'dump';
use Hash::Merge;
use feature 'say';
Hash::Merge::specify_behavior
( {
'SCALAR' => {
'SCALAR' => sub { $_[1] },
'ARRAY' => sub { [ $_[0], #{$_[1]} ] },
'HASH' => sub { $_[1] },
},
'ARRAY' => {
'SCALAR' => sub { $_[1] },
'ARRAY' => \&mergeArrays,
'HASH' => sub { $_[1] },
},
'HASH' => {
'SCALAR' => sub { $_[1] },
'ARRAY' => sub { [ values %{$_[0]}, #{$_[1]} ] },
'HASH' => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) },
},
},
'My Behavior',
);
my $h1={a=>[{aa=>1},3]};
my $h2={a=>[{bb=>2}]};
my $hMerge=Hash::Merge::merge($h1,$h2);
say "hMerge: ".dump($hMerge);
sub mergeArrays{
my ($a,$b)=#_;
my ($na,$nb)=($#$a,$#$b);
my #c;
if ($na>$nb) {
#c=#$a[($nb+1)..$na];
return mergeArrays2($a,$b,\#c,$nb);
} else {
#c=#$b[($na+1)..$nb];
return mergeArrays2($a,$b,\#c,$na);
}
}
sub mergeArrays2{
my ($a,$b,$c,$n)=#_;
my $r=[];
for my $i (0..$n) {
if (ref($a->[$i]) && ref($b->[$i])) {
push(#$r,Hash::Merge::_merge_hashes($a->[$i],$b->[$i]));
} else {
push(#$r,$a->[$i]);
}
}
push(#$r,#$c);
return $r;
}
Output:
hMerge: { a => [{ aa => 1, bb => 2 }, 3] }

The default behavior for merging arrays is to append them:
sub { [ #{$_[0]}, #{$_[1]} ] },
To get different behavior, one must use Hash::Merge::specify_behavior.
The following solution is LEFT_PRECEDENT, and merges arrays element to element:
use strict;
use warnings;
use feature qw(say);
use Data::Dump qw(dump);
use Hash::Merge qw(merge);
Hash::Merge::specify_behavior(
{ 'SCALAR' => {
'SCALAR' => sub { $_[0] },
'ARRAY' => sub { $_[0] },
'HASH' => sub { $_[0] },
},
'ARRAY' => {
'SCALAR' => sub { [ #{ $_[0] }, $_[1] ] },
'ARRAY' => sub {
my ( $left, $right ) = #_;
my #merged = #$left;
my #to_add = #$right;
for (#merged) {
last if !#to_add;
$_ = Hash::Merge::merge( $_, shift #to_add );
}
return [ #merged, #to_add ];
},
'HASH' => sub { [ #{ $_[0] }, values %{ $_[1] } ] },
},
'HASH' => {
'SCALAR' => sub { $_[0] },
'ARRAY' => sub { $_[0] },
'HASH' => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) },
},
},
'My Behavior',
);
my $h1 = { a => [ { aa => 1 }, 3 ] };
my $h2 = { a => [ { bb => 2 } ] };
my $merged = merge( $h1, $h2 );
say "Merged: " . dump($merged);
Outputs:
Merged: { a => [{ aa => 1, bb => 2 }, 3] }

Related

Perl hash add values

I am trying to push values into hash. I want to add the values under 'par3'.
e.g.
$VAR1 = { 'obj1' => ['par1',
'par2',
'par3' => ['par4','par5','par6',....]]}
I should also be able to add elements into 'par3' in case 'obj1'-'par1'-'par2'-'par3' matches.
So far I have this, but I can't figure out how can I add "the second level" under 'par3':
push #{$a{$obj}},$par1,$par2,$par3
[ ... ] is a reference to an array. Array elements are scalars. So it is not possible to directly have the structure you seem to be requesting (ie. the par3 => [ ... ] pseudocode from your question). See perldsc
It's not obvious what you are trying to do but a couple of possible ideas might be to use a reference to a hash, or to replace the array with a hash:
use Data::Dumper;
$Var2a = {
'obj1' => [
'par1',
'par2',
{ 'par3' => undef, }
],
};
push #{ $Var2a->{obj1}[2]{par3} }, 'par4', 'par5', 'par6';
print Dumper $Var2a;
$Var2b = {
'obj1' => {
'par1' => undef,
'par2' => undef,
'par3' => undef,
},
};
push #{ $Var2b->{obj1}{par3} }, 'par4', 'par5', 'par6';
print Dumper $Var2b;
I think you want to add children to par3. As such, I think you want pars to have children. If so, you need a different data structure.
# Ordered
my $obj1 = [
{ name => 'par1', children => [ ] },
{ name => 'par2', children => [ ] },
{ name => 'par3', children => [
{ name => 'par4', children => [ ] },
{ name => 'par5', children => [ ] },
{ name => 'par6', children => [ ] },
] },
];
or
# Unordered
my $obj1 = {
par1 => { },
par2 => { },
par3 => {
par4 => { },
par5 => { },
par6 => { },
},
};
To append par7 to par3's children, you would use
# Ordered
use List::Util qw( first );
my $par7 = { name => 'par7', children => [ ] };
my $par3 = first { $_->{ name } eq 'par3' } #$obj1
or die( "par3 not found" );
push #{ $par3->{ children } }, $par7;
or
# Unordered
$obj1->{ par3 }{ par7 } = { };

Unable to merge files in required format

I am trying to merge two yml files with following perl code. But data is not writing in expected format.
Code:-
use warnings;
use strict;
use feature 'say';
use Data::Dump;
use Data::Dumper;
use Storable qw(dclone);
use YAML qw(LoadFile Dump);
use File::Slurp qw(write_file);
use Hash::Merge qw(merge);
Hash::Merge::set_behavior('RETAINMENT_PRECEDENT');
die "Usage: $0 file1 file2\n" if #ARGV != 2;
my ($fname1, $fname2) = #ARGV;
my $yml1 = LoadFile($fname1);
my $yml2 = LoadFile($fname2);
my $res = merge($yml1, $yml2);
#print Dumper $res;
write_file("final.yml", Dump ($res));
Should we need to set any custom settings in perl Hash::Merge module?
Hash Merge change:-
Hash::Merge::specify_behavior(
{ SCALAR => {
SCALAR => sub { $_[1] },
ARRAY => sub { [ $_[0], #{ $_[1] } ] },
HASH => sub { $_[1] },
},
ARRAY => {
SCALAR => sub { $_[1] },
HASH => sub { $_[1] },
ARRAY => sub { # Rule for 'local_pointer'
if ( exists $_[0][0]->{data} && exists $_[1][0]->{data} && exists $_[0][0]->{name} && exists $_[1][0]->{name}){
#if ($_[0][0]->{name} eq $_[1][0]->{name}){
my #a1 = #{$_[0]};
my #a2 = #{$_[1]};
foreach my $i1 (0..$#a1) {
foreach my $i2 (0..$#a2) {
#if ($a1[$i1]->{name} eq $a2[$i2]->{name}){
#print Dumper $a1[0]->{data};
[ { data => [
$a1[$i1]->{data},
$a2[$i2]->{data},
],
name => $a2[$i2]->{name},
}
]
#else{
#}
#}
}
}
}
else {
[ #{ $_[0] }, #{ $_[1] } ] }
}
},
HASH => {
SCALAR => sub { $_[1] },
ARRAY => sub { [ values %{ $_[0] }, #{ $_[1] } ] },
HASH => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) },
},
},
'M2',
);
You can do a simple merge on these yaml snippets without Hash::Merge, example:
use strict;
use warnings;
use feature qw[say];
use DDP;
use YAML qw[LoadFile];
my $yaml = {};
my #data;
foreach my $file ( './a.yml', 'b.yml' ) {
my $y = LoadFile($file);
push #{ $yaml->{src} }, #{ $y->{src} };
foreach my $element ( #{ $y->{local_pointer} } ) {
push #data, #{ $element->{data} };
}
}
#{ $yaml->{local_pointer} } = #data;
say p $yaml;
output:
\ {
local_pointer => [
[0] {
dir => '/release/p_analysis' (U),
exe => 'bin/test.sh' (U),
tool_name => 'power_hash' (U),
type => 'exe' (U)
},
[1] {
dir => '/complete/release/1.2/55603074' (U),
name => 'code_test.config' (U),
tool_name => 'axi_ip' (U),
type => 'dir' (U)
},
[2] {
dir => '/hw/debug/test/log/' (U),
exe => 'log.pl' (U),
tool_name => 'test1' (U),
type => 'exe' (U)
},
[3] {
dir => '/releaase_test/p_analysis' (U),
exe => 'bin/test1.sh' (U),
name => 'code_test.config' (U),
tool_name => 'power1_hash' (U),
type => 'exe' (U)
}
],
src => [
[0] {
dest => 'sw_config/' (U),
ip => 'TOT' (U),
name => 'defs.yml' (U)
},
[1] {
dest => 'models/' (U),
ip => 'TOT' (U),
name => 'makebuild.vh' (U)
},
[2] {
dest => 'sw_configs/' (U),
ip => 'TOT' (U),
name => 'defs111.yml' (U)
},
[3] {
dest => 'models/' (U),
ip => 'TOT' (U),
name => 'testbuild.vh' (U)
}
]
UPDATE
Here a Hash::Merge example with a specific rule for local_pointer as in the previous example:
my #yaml = map { LoadFile( sprintf './%s.yml', $_ ) } qw[a b];
my $merger = Hash::Merge->new();
$merger->add_behavior_spec(
{ SCALAR => {
SCALAR => sub { $_[1] },
ARRAY => sub { [ $_[0], #{ $_[1] } ] },
HASH => sub { $_[1] },
},
ARRAY => {
SCALAR => sub { $_[1] },
HASH => sub { $_[1] },
ARRAY => sub { # Rule for 'local_pointer'
if ( exists $_[0][0]->{data} && exists $_[1][0]->{data} ) {
# return joined arrays into 'data'
[ { data => [
#{ $_[0][0]->{data} }, #
#{ $_[1][0]->{data} }, #
],
}
]
}
else { [ #{ $_[0] }, #{ $_[1] } ] }
}
},
HASH => {
SCALAR => sub { $_[1] },
ARRAY => sub { [ values %{ $_[0] }, #{ $_[1] } ] },
HASH => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) },
},
},
'My Behavior',
);
my $yaml = $merger->merge(#yaml);

Hash merge/concatenation

this is a dump of my hashes: %hash1
$VAR1 = {
abc => {
123 => [
'xx',
'yy',
'zy'
],
456 => [
'ab',
'cd',
'ef'
]
}
};
and the second one: %hash2
$VAR2 = {
def => {
659 => [
'wx',
'yg',
'kl'
],
456 => [
'as',
'sd',
'df'
]
},
abc => {
987 => [
'lk',
'dm',
'sd'
]
}
};
Now I want to merge these two hashes in a new hash, but if a key is duplicated (here 'abc'), the values should be appended, not replaced, so the keys should remain unique, and all the values should be retained as well. How can this be done in Perl?
The output should be as follows:
$VAR1 = {
def => {
659 => [
'wx',
'yg',
'kl'
],
456 => [
'as',
'sd',
'df'
]
},
abc => {
987 => [
'lk',
'dm',
'sd'
],
123 => [
'xx',
'yy',
'zy'
],
456 => [
'ab',
'cd',
'ef'
]
}
};
Use the CPAN modules Hash::Merge or Hash::Merge::Simple. The first is highly configurable and the second is very simple to use.
for my $x (keys(%h2)) {
for my $y (keys(%{ $h2{$x} })) {
push #{ $h1{$x}{$y} }, #{ $h2{$x}{$y} };
}
}
For the sample data provided, the following would perform the merging you describe:
my %merged = map {
$_ => {
%{$a{$_} // {}},
%{$b{$_} // {}}
}
} ( keys %a, keys %b );
Test:
use strict;
use warnings;
use Data::Dump 'dd';
my %a = (
abc => {
123 => [
'xx',
'yy',
'zy'
],
456 => [
'ab',
'cd',
'ef'
]
}
);
my %b = (
def => {
659 => [
'wx',
'yg',
'kl'
],
456 => [
'as',
'sd',
'df'
]
},
abc => {
987 => [
'lk',
'dm',
'sd'
]
}
);
my %merged = map {
$_ => {
%{$a{$_} // {}},
%{$b{$_} // {}}
}
} ( keys %a, keys %b );
dd \%merged;
# {
# abc => {
# 123 => ["xx", "yy", "zy"],
# 456 => ["ab", "cd", "ef"],
# 987 => ["lk", "dm", "sd"],
# },
# def => { 456 => ["as", "sd", "df"], 659 => ["wx", "yg", "kl"] },
# }
sub merge_hashes {
my ($h1, $h2) = #_;
foreach my $key (keys %$h2) {
if (!exists $h1->{$key} || ref($h1->{$key}) ne 'HASH' || ref($h2->{$key}) ne 'HASH') {
$h1->{$key} = $h2->{$key};
}
else {
merge_hashes($h1->{$key}, $h2->{$key});
}
}
}
merge_hashes(\%hash1, \%hash2);

Search in LDAP with conditions?

When I do
#!/usr/bin/perl -w
use strict;
use Net::LDAP;
use Data::Dumper;
my $dn="...";
my $password="xxx";
my $ldap = Net::LDAP->new('example.com') or die "$#";
my $mesg = $ldap->bind($dn, password => $password);
if ($mesg->code) { die "uuuu $mesg"; }
$mesg = $ldap->search(
base => "dc=example,dc=com",
filter => "(name=LIST)",
);
print Dumper $mesg;
I get
$VAR1 = bless( {
'parent' => bless( {
...
}, 'Net::LDAP' ),
'entries' => [
bless( {
'changes' => [],
'changetype' => 'modify',
'asn' => {
'objectName' => 'CN=LIST,OU=test group,OU=M,OU=I,DC=example,DC=com',
'attributes' => [
{
'type' => 'objectClass',
'vals' => [
'top',
'group'
]
},
{
'type' => 'cn',
'vals' => [
'LIST'
]
},
{
'type' => 'member',
'vals' => [
'CN=user1,OU=BaseUsers,DC=example,DC=com',
'CN=user2,OU=BaseUsers,DC=example,DC=com',
]
},
...
where I would only like to output those from member that have in their object
objectCategory: CN=Person,CN=Schema,CN=Configuration,DC=example,DC=com
Does anyone know how to do that?
foreach my $entry (#{$mesg->{'entries'}})
{
my $match = 0;
my $name = $entry->{'asn'}->{'objectName'};
foreach my $attr (#{$entry->{'asn'}->{'attributes'}})
{
if('member' eq $attr->{'type'})
{
foreach my $val (#{$attr->{'vals'}})
{
if($val =~ /^CN=.*,CN=.*,CN=.*,DC=example,DC=com$/)
{
$match = 1;
last;
}
}
}
}
if($match)
{
print $name;
}
}
For your example data above this will return no matches since none of the "members" match the search pattern you specified. Also I wasn't sure if you wanted to output the object name (as per my code) or the matching string. If the latter you don't need $match, simply put a print in the innermost block.

How can I merge several hashes into one hash in Perl?

In Perl, how do I get this:
$VAR1 = { '999' => { '998' => [ '908', '906', '0', '998', '907' ] } };
$VAR1 = { '999' => { '991' => [ '913', '920', '918', '998', '916', '919', '917', '915', '912', '914' ] } };
$VAR1 = { '999' => { '996' => [] } };
$VAR1 = { '999' => { '995' => [] } };
$VAR1 = { '999' => { '994' => [] } };
$VAR1 = { '999' => { '993' => [] } };
$VAR1 = { '999' => { '997' => [ '986', '987', '990', '984', '989', '988' ] } };
$VAR1 = { '995' => { '101' => [] } };
$VAR1 = { '995' => { '102' => [] } };
$VAR1 = { '995' => { '103' => [] } };
$VAR1 = { '995' => { '104' => [] } };
$VAR1 = { '995' => { '105' => [] } };
$VAR1 = { '995' => { '106' => [] } };
$VAR1 = { '995' => { '107' => [] } };
$VAR1 = { '994' => { '910' => [] } };
$VAR1 = { '993' => { '909' => [] } };
$VAR1 = { '993' => { '904' => [] } };
$VAR1 = { '994' => { '985' => [] } };
$VAR1 = { '994' => { '983' => [] } };
$VAR1 = { '993' => { '902' => [] } };
$VAR1 = { '999' => { '992' => [ '905' ] } };
to this:
$VAR1 = { '999:' => [
{ '992' => [ '905' ] },
{ '993' => [
{ '909' => [] },
{ '904' => [] },
{ '902' => [] }
] },
{ '994' => [
{ '910' => [] },
{ '985' => [] },
{ '983' => [] }
] },
{ '995' => [
{ '101' => [] },
{ '102' => [] },
{ '103' => [] },
{ '104' => [] },
{ '105' => [] },
{ '106' => [] },
{ '107' => [] }
] },
{ '996' => [] },
{ '997' => [ '986', '987', '990', '984', '989', '988' ] },
{ '998' => [ '908', '906', '0', '998', '907' ] },
{ '991' => [ '913', '920', '918', '998', '916', '919', '917', '915', '912', '914' ] }
]};
I think this is closer than anybody else has gotten:
This does most of what you want. I did not store things in arrays of singular
hashes, as I don't feel that that is useful.
Your scenario is not a regular one. I've tried to genericize this to some extent,
but was not possible to overcome the singularity of this code.
First of all because it appears you want to collapse everything with the same
id into a merged entity (with exceptions), you have to descend through the structure
pulling the definitions of the entities. Keeping track of levels, because you
want them in the form of a tree.
Next, you assemble the ID table, merging entities as possible. Note that you
had 995 defined as an empty array one place and as a level another. So given
your output, I wanted to overwrite the empty list with the hash.
After that, we need to move the root to the result structure, descending that in order
to assign canonical entities to the identifiers at each level.
Like I said, it's not anything that regular. Of course, if you still want a list
of hashes which are no more than pairs, that's an exercise left to you.
use strict;
use warnings;
# subroutine to identify all elements
sub descend_identify {
my ( $level, $hash_ref ) = #_;
# return an expanding list that gets populated as we desecend
return map {
my $item = $hash_ref->{$_};
$_ => ( $level, $item )
, ( ref( $item ) eq 'HASH' ? descend_identify( $level + 1, $item )
: ()
)
;
} keys %$hash_ref
;
}
# subroutine to refit all nested elements
sub descend_restore {
my ( $hash, $ident_hash ) = #_;
my #keys = keys %$hash;
#$hash{ #keys } = #$ident_hash{ #keys };
foreach my $h ( grep { ref() eq 'HASH' } values %$hash ) {
descend_restore( $h, $ident_hash );
}
return;
}
# merge hashes, descending down the hash structures.
sub merge_hashes {
my ( $dest_hash, $src_hash ) = #_;
foreach my $key ( keys %$src_hash ) {
if ( exists $dest_hash->{$key} ) {
my $ref = $dest_hash->{$key};
my $typ = ref( $ref );
if ( $typ eq 'HASH' ) {
merge_hashes( $ref, $src_hash->{$key} );
}
else {
push #$ref, $src_hash->{$key};
}
}
else {
$dest_hash->{$key} = $src_hash->{$key};
}
}
return;
}
my ( %levels, %ident_map, %result );
#descend through every level of hash in the list
# #hash_list is assumed to be whatever you Dumper-ed.
my #pairs = map { descend_identify( 0, $_ ); } #hash_list;
while ( #pairs ) {
my ( $key, $level, $ref ) = splice( #pairs, 0, 3 );
$levels{$key} |= $level;
# if we already have an identity for this key, merge the two
if ( exists $ident_map{$key} ) {
my $oref = $ident_map{$key};
my $otyp = ref( $oref );
if ( $otyp ne ref( $ref )) {
# empty arrays can be overwritten by hashrefs -- per 995
if ( $otyp eq 'ARRAY' && #$oref == 0 && ref( $ref ) eq 'HASH' ) {
$ident_map{$key} = $ref;
}
else {
die "Uncertain merge for '$key'!";
}
}
elsif ( $otyp eq 'HASH' ) {
merge_hashes( $oref, $ref );
}
else {
#$oref = sort { $a <=> $b || $a cmp $b } keys %{{ #$ref, #$oref }};
}
}
else {
$ident_map{$key} = $ref;
}
}
# Copy only the keys that do not appear at higher levels to the
# result hash
if ( my #keys = grep { !$levels{$_} } keys %ident_map ) {
#result{ #keys } = #ident_map{ #keys } if #keys;
}
# then step through the hash to make sure that the entries at
# all levels are equal to the identity
descend_restore( \%result, \%ident_map );
Use CPAN! Try Hash::Merge
# OO interface.
my $merge = Hash::Merge->new( 'LEFT_PRECEDENT' );
my %c = %{ $merge->merge( \%a, \%b ) };
See CPAN for more info, it pretty much does everything you would want to, and is fully customizable.
Give this recursive solution a try:
# XXX: doesn't handle circular problems...
sub deepmerge {
my (#structs) = #_;
my $new;
# filter out non-existant structs
#structs = grep {defined($_)} #structs;
my $ref = ref($structs[0]);
if (not all(map {ref($_) eq $ref} #structs)) {
warn("deepmerge: all structs are not $ref\n");
}
my #tomerge = grep {ref($_) eq $ref} #structs;
return qr/$tomerge[0]/ if scalar(#tomerge) == 1 and $ref eq 'Regexp';
return $tomerge[0] if scalar(#tomerge) == 1;
if ($ref eq '') {
$new = pop(#tomerge); # prefer farthest right
}
elsif ($ref eq 'Regexp') {
$new = qr/$tomerge[$#tomerge]/;
}
elsif ($ref eq 'ARRAY') {
$new = [];
for my $i (0 .. max(map {scalar(#$_) - 1} #tomerge)) {
$new->[$i] = deepmerge(map {$_->[$i]} #tomerge);
}
}
elsif ($ref eq 'HASH') {
$new = {};
for my $key (uniq(map {keys %$_} #tomerge)) {
$new->{$key} = deepmerge(map {$_->{$key}} #tomerge);
}
}
else {
# ignore all other structures...
$new = '';
}
return $new;
}
Modify it to your hearts content to achieve the desired result.
Upon further investigation, I noticed you're merging them in some different way than the above algorithm. Maybe just use this as an example then. Mine does this:
deepmerge({k => 'v'}, {k2 => 'v2'});
# returns {k => 'v', k2 => 'v2'}
And similar things for arrays.
I indented your wanted output as it was hard to read, for the benefit of other people who want to answer. I'm still thinking of an answer.
$VAR1 = { '999:' => [
{ '992' => [ '905' ] },
{ '993' => [
{ '909' => [] },
{ '904' => [] },
{ '902' => [] }
]
},
{ '994' => [
{ '910' => [] },
{ '985' => [] },
{ '983' => [] }
]
},
{ '995' => [
{ '101' => [] },
{ '102' => [] },
{ '103' => [] },
{ '104' => [] },
{ '105' => [] },
{ '106' => [] },
{ '107' => [] }
]
},
{ '996' => [] },
{ '997' => [ '986', '987', '990', '984', '989', '988' ] },
{ '998' => [ '908', '906', '0', '998', '907' ] },
{ '991' => [ '913', '920', '918', '998', '916', '919', '917', '915', '912', '914' ] }
]
};
I don't see the point of all those single entry hashes though, would not the following be better?
$VAR1 = { '999:' => {
'992' => [ '905' ],
'993' => {
'909' => [],
'904' => [],
'902' => []
},
'994' => {
'910' => [],
'985' => [],
'983' => []
},
'995' => {
'101' => [],
'102' => [],
'103' => [],
'104' => [],
'105' => [],
'106' => [],
'107' => []
},
'996' => [],
'997' => [ '986', '987', '990', '984', '989', '988' ],
'998' => [ '908', '906', '0', '998', '907' ],
'991' => [ '913', '920', '918', '998', '916', '919', '917', '915', '912', '914' ]
}
};
Assuming the above data is in a file dump.txt, you can eval it piece by piece.
Updated code below
use strict;
use File::Slurp;
my $final_data = {};
my #data = map {eval $_} (read_file("dump.txt") =~ /\$VAR1 = ([^;]+);/gs);
foreach my $element (#data) {
my $key = (keys %$element)[0];
$final_data->{$key} ||= [];
push #{$final_data->{$key}}, $element->{$key}
};
use Data::Dumper;
print Data::Dumper->Dump([$final_data]);
If you want to completely deep merge, you can at the end pass $final_data through this (not tested!!!) deep merger:
# Merge an array of hashes as follows:
# IN: [ { 1 => 11 }, { 1 => 12 },{ 2 => 22 } ]
# OUT: { 1 => [ 11, 12 ], 2 => [ 22 ] }
# This is recursive - if array [11,12] was an array of hashrefs, we merge those too
sub merge_hashes {
my $hashes = #_[0];
return $hashes unless ref $hashes eq ref []; # Hat tip to brian d foy
return $hashes unless grep { ref #_ eq ref {} } #$hashes; # Only merge array of hashes
my $final_hashref = {};
foreach my $element (#$hashes) {
foreach my $key (keys %$element) {
$final_hashref->{$key} ||= [];
push #{ $final_hashref->{$key} }, $element->{$key};
}
}
foreach my $key (keys %$final_hashref) {
$final_hashref->{$key} = merge_hashes($final_hashref->{$key});
}
return $final_hashref;
}
Use push and autovivification.
Start with the usual front matter:
#! /usr/bin/perl
use warnings;
use strict;
Read your sample input from the DATA filehandle and create a datastructure similar to the one you dumped:
my #hashes;
while (<DATA>) {
my $VAR1;
$VAR1 = eval $_;
die $# if $#;
push #hashes => $VAR1;
}
Your input has two cases:
A reference to an array that contains data to be merged with its cousins that have the same "key path."
Otherwise, it's a reference to a hash that contains a reference to an array from case 1 at some depth, so we strip off the outermost layer and keep digging.
Note the use of $_[0]. The semantics of Perl subroutines are such that the values in #_ are aliases rather than copies. This lets us call merge directly without having to first create a bunch of scaffolding to hold the merged contents. The code will break if you copy the value instead.
sub merge {
my $data = shift;
if (ref($data) eq "ARRAY") {
push #{ $_[0] } => #$data;
}
else {
foreach my $k (%$data) {
merge($data->{$k} => $_[0]{$k});
}
}
}
Now we walk #hashes and incrementally merge their contents into %merged.
my %merged;
foreach my $h (#hashes) {
foreach my $k (keys %$h) {
merge $h->{$k} => $merged{$k};
}
}
We don't know in what order the values arrived, so run a final cleanup pass to sort the arrays:
sub sort_arrays {
my($root) = #_;
if (ref($root) eq "ARRAY") {
#$root = sort { $a <=> $b } #$root;
}
else {
sort_arrays($root->{$_}) for keys %$root;
}
}
sort_arrays \%merged;
The Data::Dumper module is great for quick debugging!
use Data::Dumper;
$Data::Dumper::Indent = 1;
print Dumper \%merged;
Place a copy of the input from your question into the special DATA filehandle:
__DATA__
$VAR1 = { '999' => { '998' => [ '908', '906', '0', '998', '907' ] } };
$VAR1 = { '999' => { '991' => [ '913', '920', '918', '998', '916', '919', '917', '915', '912', '914' ] } };
$VAR1 = { '999' => { '996' => [] } };
$VAR1 = { '999' => { '995' => [] } };
$VAR1 = { '999' => { '994' => [] } };
$VAR1 = { '999' => { '993' => [] } };
$VAR1 = { '999' => { '997' => [ '986', '987', '990', '984', '989', '988' ] } };
$VAR1 = { '995' => { '101' => [] } };
$VAR1 = { '995' => { '102' => [] } };
$VAR1 = { '995' => { '103' => [] } };
$VAR1 = { '995' => { '104' => [] } };
$VAR1 = { '995' => { '105' => [] } };
$VAR1 = { '995' => { '106' => [] } };
$VAR1 = { '995' => { '107' => [] } };
$VAR1 = { '994' => { '910' => [] } };
$VAR1 = { '993' => { '909' => [] } };
$VAR1 = { '993' => { '904' => [] } };
$VAR1 = { '994' => { '985' => [] } };
$VAR1 = { '994' => { '983' => [] } };
$VAR1 = { '993' => { '902' => [] } };
$VAR1 = { '999' => { '992' => [ '905' ] } };
A sample of the output is below:
'994' => {
'910' => [],
'985' => [],
'983' => []
},
'999' => {
'993' => [],
'992' => [
'905'
],
'997' => [
'984',
'986',
'987',
'988',
'989',
'990'
],
wow. thanks so much everyone (especially Axeman)! sorry for the lack of code or clarification, I was trying to generate a tree, and did try Hash::Merge, but could not for the life of me resolve the coined-995 problem of replacing the empty 995 with the non-empty 995; Axeman's solution works beautifully and I really appreciate the help/collaboration! (also tried the others and it either did the same thing as Hash::Merge, or it actually got rid of some branches).
some background on the input: had a set of hashes, each had keys (all same level) and two of which defined a) a parent to another, and b) itself (the rest were children), and so with a tree, i figured a hash was perfect, came up with a set of new hashes {a}->{b}->[c], and here we are...
again, thanks everyone and Axeman!