use strict; use warnings; use Test::More; use Test::Deep; use utf8; my ( $v, $w, $x, $y ) = get_data(); my @wanted = map { bagify($_) } @{ $v }; cmp_deeply( $w, bag( @wanted ), 'OP data' ); my @deeper = map { bagify($_) } @{ $x }; cmp_deeply( $y, bag( @deeper ), 'deeper nesting' ); done_testing; sub bagify { my $input = shift; my $output; if ( ref $input eq 'HASH' ) { while ( my ( $key, $val ) = each %{ $input } ) { $output->{ $key } = bagify($val); } } elsif ( ref $input eq 'ARRAY' ) { $output = bag( map { bagify($_) } @{ $input } ); } else { $output = $input; } return $output; } sub get_data { my @v = ( {'platformid' => '100','da' => 'A.9','os' => 'microsoft amd64 wNT-6.1-S','ma' => 'A.9','cc' => 'A.9','size' => [{'objecttype' => 'OB2BAR','totalsize' => '230986 KB','application' => 'IDB','hostname' => '5096'},{'objecttype' => 'WINFS','totalsize' => '1262152 KB','application' => 'R: [New Volume]','hostname' => '5096'},{'objecttype' => 'WINFS','totalsize' => '574463 KB','application' => 'C:','hostname' => '5096'}],'objecttype' => '6','host' => '5096'}, {'platformid' => '22','da' => 'A.9','os' => 'hp-ux-11.31','host' => '2060','cc' => 'A.9','ma' => 'A.9','size' => [{'objecttype' => 'FILESYSTEM','totalsize' => '3628129 KB','application' => '/depot','hostname' => '2060'}],'objecttype' => '2'} ); my @w = ( {'platformid' => '22','da' => 'A.9','os' => 'hp-ux-11.31','host' => '2060','cc' => 'A.9','ma' => 'A.9','size' => [{'objecttype' => 'FILESYSTEM','totalsize' => '3628129 KB','application' => '/depot','hostname' => '2060'}],'objecttype' => '2'}, {'platformid' => '100','da' => 'A.9','os' => 'microsoft amd64 wNT-6.1-S','ma' => 'A.9','cc' => 'A.9','size' => [{'objecttype' => 'OB2BAR','totalsize' => '230986 KB','application' => 'IDB','hostname' => '5096'},{'objecttype' => 'WINFS','totalsize' => '1262152 KB','application' => 'R: [New Volume]','hostname' => '5096'},{'objecttype' => 'WINFS','totalsize' => '574463 KB','application' => 'C:','hostname' => '5096'}],'objecttype' => '6','host' => '5096'} ); my @x = ( { a => { uc => 'A', accented => [qw/á à/] }, b => 'B', c => [ { foo => 'bar' }, { baz => [qw/q u x/, { nested => [qw/even more/] }] } ] }, { d => ['D', [qw/nested array/] ], e => 'E', f => [ { xyz => 'abc' }, { zzz => 'ZZZ' } ] }, ); my @y = ( { e => 'E', f => [ { zzz => 'ZZZ' }, { xyz => 'abc' } ], d => [ [qw/array nested/], 'D'] }, { a => { uc => 'A', accented => [qw/à á/] }, c => [ { baz => [ { nested => [qw/more even/] }, qw/x u q/] }, { foo => 'bar' } ], b => 'B' }, ); return ( \@v, \@w, \@x, \@y ); }