Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Data::Rmap to modify an arrayref of arrayrefs

by kcott (Archbishop)
on Jul 02, 2011 at 02:14 UTC ( [id://912429]=note: print w/replies, xml ) Need Help??


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

I haven't used Data::Rmap before so others may have better answers. However, the following works and solves your problem.

use strict; use warnings; use Data::Rmap qw(:all); my $initial = [ note => [ shopping => [ 'item' ] ] ]; use Data::Dumper; # build a list of shopping items ** for reuse ** my @shopping_items; for my $item qw(bread butter beans) { push @shopping_items, [ item => $item ]; } my ($dump) = rmap_array { # If we get an arrayref whose first element is 'shopping' if ($_->[0] eq 'shopping') { # Make the second element the shopping list $_->[1] = [ @shopping_items ]; # No need to drill down any further cut($_); } else { # if the arrayrefs first element is not 'shopping' # then simply pass it through $_; } } $initial; warn Dumper($initial, $dump);

-- Ken

Replies are listed 'Best First'.
Re^2: Data::Rmap to modify an arrayref of arrayrefs
by metaperl (Curate) on Jul 03, 2011 at 00:25 UTC
    Thanks kcott. I've gone ahead and abstracted htis into a function. Now, I'm going to try to solve it at the level of 'items' instead of at the parent level.
    use strict; use warnings; use Data::Rmap qw(:all); use Data::Dumper; my $initial_lol = [ note => [ shopping => [ item => 'sample' ] ] ]; # In $initial_lol, the new child of shopping is shopping_items my $new_lol = newchild($initial_lol, shopping => shopping_items()); warn Dumper($new_lol); sub newchild { my ($lol, $parent_arrayref_label, $new_child)=@_; my ($mapresult) = rmap_array { if ($_->[0] eq $parent_arrayref_label) { $_->[1] = $new_child; cut($_); } else { $_; } } $lol; $mapresult; } sub shopping_items { my @shopping_items; for my $item qw(bread butter beans) { push @shopping_items, [ item => $item ]; } \@shopping_items; }




    The mantra of every experienced web application developer is the same: thou shalt separate business logic from display. Ironically, almost all template engines allow violation of this separation principle, which is the very impetus for HTML template engine development.

    -- Terence Parr, "Enforcing Strict Model View Separation in Template Engines"

      tada! now I work at the item level instead of it's parent:
      use strict; use warnings; use Data::Rmap qw(:all); use Data::Dumper; my $initial_lol = [ note => [ shopping => [ item => 'sample' ] ] ]; # In $initial_lol, the new child of shopping is shopping_items my $new_lol = newnode($initial_lol, item => shopping_items()); warn Dumper($new_lol); sub newnode { my ($lol, $node_label, $new_node)=@_; my ($mapresult) = rmap_array { if ($_->[0] eq $node_label) { $_ = shopping_items(); cut($_); } else { $_; } } $lol; $mapresult; } sub shopping_items { my @shopping_items; for my $item qw(bread butter beans) { push @shopping_items, [ item => $item ]; } \@shopping_items; }




      The mantra of every experienced web application developer is the same: thou shalt separate business logic from display. Ironically, almost all template engines allow violation of this separation principle, which is the very impetus for HTML template engine development.

      -- Terence Parr, "Enforcing Strict Model View Separation in Template Engines"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://912429]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 12:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found