sciurius has asked for the wisdom of the Perl Monks concerning the following question:
Assume you have a (big) perl data structure, a hash, of which all keys have values that can be scalars, arrays or hashes. Arrays and hashes can contain scalars, arrays or hashes, and so on.
For example:
$orig = { a => { b => [ 'c', 'd' ], e => [ [ 'f' ] ] } };
Assume there is an operation 'augment' that takes this data structure and another (smaller) structure. The original data structure will be modified so that all the data from the smaller structure is now in the original structure as well.
For example, augmenting the strucure above with
{ a => { e => [ [ 'g' ] ] } }
will yield
{ a => { b => [ 'c', 'd' ], e => [ [ 'g' ] ] } }
What I'm looking for is the reversed operation, let's call it 'reduce', that takes the original and augmented structures as arguments, and returns the (minimal) structure that is needed to augment the original.
I'm sure that there are modules that already implement this but I haven't been able to identify them...
A cut/paste ready test program:
use strict; use Test::More tests => 2; use Storable qw(dclone); my $orig = { a => { b => [ 'c', 'd' ], e => [ [ 'f' ] ] } }; my $new = dclone($orig); my $delta = { a => { e => [ [ 'g' ] ] } }; augment( $new, $delta ); my $augmented = { a => { b => [ 'c', 'd' ], e => [ [ 'g' ] ] } }; is_deeply( $new, $augmented, "augment" ); reduce( $new, $orig ); is_deeply( $new, $delta, "reduce" );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Augmenting and reducing data structures
by hippo (Archbishop) on Apr 22, 2021 at 13:04 UTC | |
|
Re: Augmenting and reducing data structures
by tybalt89 (Monsignor) on Apr 22, 2021 at 23:55 UTC | |
|
Re: Augmenting and reducing data structures
by LanX (Saint) on Apr 22, 2021 at 12:36 UTC | |
by LanX (Saint) on Apr 22, 2021 at 17:00 UTC | |
by sciurius (Beadle) on Apr 23, 2021 at 12:02 UTC | |
by LanX (Saint) on Apr 23, 2021 at 16:17 UTC | |
by sciurius (Beadle) on Apr 24, 2021 at 13:25 UTC | |
| |
by LanX (Saint) on Apr 22, 2021 at 17:15 UTC | |
by choroba (Cardinal) on Apr 22, 2021 at 17:18 UTC | |
by choroba (Cardinal) on Apr 22, 2021 at 17:24 UTC | |
| |
by LanX (Saint) on Apr 22, 2021 at 17:20 UTC | |
by sciurius (Beadle) on Apr 23, 2021 at 07:30 UTC | |
by LanX (Saint) on Apr 23, 2021 at 12:39 UTC | |
|
Re: Augmenting and reducing data structures
by bliako (Abbot) on Apr 22, 2021 at 13:00 UTC | |
by Fletch (Bishop) on Apr 23, 2021 at 16:53 UTC | |
by bliako (Abbot) on Apr 27, 2021 at 07:59 UTC | |
by Fletch (Bishop) on Apr 27, 2021 at 12:50 UTC | |
|
Re: Augmenting and reducing data structures
by perlfan (Parson) on Apr 22, 2021 at 17:13 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |