Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^4: Copy an hash modifying some selected values

by Anonymous Monk
on Nov 08, 2018 at 08:36 UTC ( #1225393=note: print w/replies, xml ) Need Help??

in reply to Re^3: Copy an hash modifying some selected values
in thread Copy an hash modifying some selected values

Thank you!
I still get an error with your version of the routine so I will use Data::Diver.
I would prefer your routine since it is pure Perl... just in case you would want to fix my example below.
use strict; use Data::Dumper; use Data::Diver qw( DiveVal ); use Storable 'dclone'; my %hash1 = ( key1 => 'relative_path', key2 => { key21 => 'another_relative_path', key22 => '/an_absolute_path', key23 => 'relative_path', }, key3 => '', key4 => 4 ); my $hash2 = dclone \%hash1; my $hash3 = dclone \%hash1; my $home = '/home'; my @paths = ('key1', 'key2:key22', 'key2:key23'); for my $path (@paths) { my @path = split(/:/, $path); my $val = dive(\%hash1, @path); my $new_val = (substr($val,0,1) eq '/') ? $val : "$home/$val"; DiveVal($hash2, map \$_, @path) = $new_val; # dive_val($hash3, map \$_, @path) = $new_val; } die Dumper \@paths,\%hash1,$hash2; sub dive { my $r = shift; $r = $r->{shift(@_)} while $r && @_; return $r; } sub dive_val :lvalue { my $p = \shift; my $r = \( $$p->{$_} ) for @_; $$p }

Replies are listed 'Best First'.
Re^5: Copy an hash modifying some selected values
by ikegami (Pope) on Nov 08, 2018 at 10:26 UTC

    $r should be $p. Should have been obvious since strict catches that error. Fixed in original.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2019-08-17 21:15 GMT
Find Nodes?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?

    Results (134 votes). Check out past polls.