Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
enough theory, now practical code:

use strict; use warnings; use Data::Dump qw/pp/; use Scalar::Util qw/reftype/; sub walk { my ($entry,$code) =@_; my $type = reftype($entry); $type //= "SCALAR"; if ($type eq "HASH") { walk($_,$code) for values %$entry; } elsif ($type eq "ARRAY") { walk($_,$code) for @$entry; } elsif ($type eq "SCALAR" ) { $code->($_[0]); # alias of entry } else { warn "unknown type $type"; } } my $test = { a => [2, 3, [4, { b => 42 }]], c => 5 }; pp $test; walk $test, sub { $_[0]+=100 }; pp $test;


/usr/bin/perl -w /tmp/ { a => [2, 3, [4, { b => 42 }]], c => 5 } { a => [102, 103, [104, { b => 142 }]], c => 105 }

Cheers Rolf

( addicted to the Perl Programming Language)


of course you could extend walk to call optional $coderefs on every node and specify them as named parameters:

 walk $ref , SCALAR => sub { print $_[0] } , ARRAY => sub { Dump $_[0] }, ...

But I think the original code is so small thats its easier to copy and manipulate it in place for different needs.

In reply to Re^2: Manipulate deepest values in complex hash structures by LanX
in thread Manipulate deepest values in complex hash structures by RecursionBane

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [Corion]: erix: The main thing holding me back from that is setting up a (throwaway) Linuxish environment to run the script. Usually I use the Google Cloud Shell for that, but something didn't work out right the last time I tried to use it for this
    [Corion]: Andd using the Linux-For-Windows thing isn't great for throwaway environments either, and I'm too lazy to set up VMware or whatever ;)
    [Discipulus]: lazy? virtualbox is pretty no-setup software

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (5)
    As of 2018-07-23 11:50 GMT
    Find Nodes?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

      Results (465 votes). Check out past polls.