in reply to Data::Rmap to modify an arrayref of arrayrefs

The trick seems to be to replace the 'items' sub-element:

use strict; use warnings; use Data::Dump qw(); use Data::Rmap qw(); my $initial = [note => [shopping => ['item']]]; Data::Rmap::rmap_array { return $_ if $_->[0] ne 'shopping'; # Ignore non-shopping entries my @item= map {[item => $_]} qw(bread butter beans); $_->[1] = ['shopping', @item]; Data::Rmap::cut ($_); } $initial; print Data::Dump::dump ($initial), "\n";

Prints:

[ "note", [ "shopping", [ "shopping", ["item", "bread"], ["item", "butter"], ["item", "beans"], ], ], ]
True laziness is hard work

Replies are listed 'Best First'.
Re^2: Data::Rmap to modify an arrayref of arrayrefs
by metaperl (Curate) on Jul 02, 2011 at 10:23 UTC

      The structure may not be right, but the sense is and I see your solution takes elements from my code so it wasn't all loss was it? You may find the following trivial variant more to your liking:

      use strict; use warnings; use Data::Dump qw(); use Data::Rmap qw(); my $initial = [note => [shopping => ['item']]]; Data::Rmap::rmap_array { return $_ if $_->[0] ne 'shopping'; # Ignore non-shopping entries my @item= map {[item => $_]} qw(bread butter beans); $_ = ['shopping', @item]; Data::Rmap::cut ($_); } $initial; print Data::Dump::dump ($initial), "\n";

      prints:

      [ "note", [ "shopping", ["item", "bread"], ["item", "butter"], ["item", "beans"], ], ]
      True laziness is hard work