This is slightly more complicated than your average hash walker, since we need to know how to modify the parent. This solution passes around an $extra hash with information on how to update the parent. $extra is expected to have keys parent and key. Based on the ref of parent, we update it as a hash or an array. We could make this a bit cleaner by a closure as the extra argument, and we call the closure when we find a match.
#!/usr/bin/perl -w use strict; use Data::Dumper; use Test::More tests => 1; my $element = { 'check' => { 'field' => 'OLK_BO_BK_TYPE' }, 'type' => 'case', 'case' => [ { 'value' => '1', 'resultset' => 'BROKER' }, { 'value' => '3', 'resultset' => { 'field' => 'OLK_BO_BK_DESC' }, }, ] }; my $expected = { 'check' => 'OLK_BO_BK_TYPE', 'type' => 'case', 'case' => [ { 'value' => '1', 'resultset' => 'BROKER' }, { 'value' => '3', 'resultset' => 'OLK_BO_BK_DESC', }, ] }; hash_walk($element); is_deeply( $element, $expected, "hash_walk worked as expected"); sub hash_walk { my ( $element, $extra ) = @_; my ( $parent, $pkey ); if ($extra) { $parent = $extra->{parent}; $pkey = $extra->{key}; } if ( ref($element) =~ /HASH/ ) { foreach my $key ( keys %$element ) { if ( $key =~ /field/ ) { if ( ref($parent) eq 'HASH' ) { $parent->{$pkey} = $element->{$key}; } elsif ( ref( $parent eq 'ARRAY' ) ) { $parent->[$pkey] = $element->{$key}; } else { warn("I don't know what to do with field in parent +[$parent]" ); } } else { hash_walk( $element->{$key}, { parent => $element, key => $key } ); } } } elsif ( ref($element) =~ /ARRAY/ ) { my $upper = @$element - 1; foreach my $i ( 0 .. $upper ) { my $index = $element->[$i]; hash_walk( $index, { parent => $element, key => $i } ); } }

In reply to Re: replace a key with its value by spazm
in thread replace a key with its value by micmac

Title:
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, details, 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, summary, 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.