Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
To get the output you requested, I had to make several assumptions:
  1. The %filer_device and %filer_volume type of subhashes are of depth 1 (i.e. @west and @ridge are not structures).
  2. %defaults are special and their name must be hardcoded.

#!/usr/bin/perl use warnings; use strict; use v5.14; # Hashrefs for keys and values. use JSON; use Data::Dumper; sub recurse_hash { my ($hash, $found, @findkeys) = @_; for my $key (sort { # Defaults go first. local $a = q() if '%default' eq $a; local $b = q() if '%default' eq $b; $a cmp $b; } keys %{$hash}) { my $value = $hash->{$key}; if ('HASH' eq ref $value) { $found = {%$found, %{ recurse_hash($value, $found, @findke +ys) }}; } for my $find (@findkeys) { if ($key eq $find) { if ('HASH' eq ref $value) { $found->{$key} = [ map $value->{$_}, sort keys $va +lue ]; } else { $found->{$key} = $value; } } } } if (@findkeys == keys $found) { my $count; $count = @{ (grep 'ARRAY' eq ref $_, values $found)[0] // [] } +; $count ||= 1; for my $i (0 .. $count - 1) { print join(':', map { 'ARRAY' eq ref $_ ? $_->[$i] : $_ } @{$found}{@findkeys}), "\n"; } print "\n"; $found = {}; } return $found; } my @keylist = qw(%export_name %filer_device %filer_volume %mount_acl %mount_group %mount_user %mount_opts); my $string = do { local $/; <DATA> }; my $struct = decode_json($string); recurse_hash($struct, {}, @keylist); __DATA__ { "%name": "Global Central Configuration file", "%description": "A representation of nfs mount points for dev mach +ines", "%schema": "schema.conf", "@dev": { "home_nfs": { "%default": { "%mount_opts": "nfsvers=3,timeo=600,retrans=2", "%mount_user": "root", "%mount_group": "root", "%mount_acl": "0755" }, "%comment": "----Home NFS Directories----", "home-lnk-mpt": { "%export_name": "/links", "%filer_device": { "@west": "nydevnfs_links", "@ridge": "rnap7750-s" }, "%filer_volume": { "@west": "/vol/links", "@ridge": "/vol/links_c" } }, "home7_mpt": { "%export_name": "/home7", "%filer_device": { "@west": "nydevnfs_home7", "@ridge": "rnap7751-s" }, "%filer_volume": { "@west": "/vol/home7", "@ridge": "/vol/home7_c" } }, "home8_mpt": { "%export_name": "/home8", "%filer_device": { "@west": "nydevnfs_home2", "@ridge": "rnap2114-s" }, "%filer_volume": { "@west": "/vol/home2", "@ridge": "/vol/home2_c" } } } } }
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

In reply to Re: Traversing a complex data structure searching for certain keys and printing their values by choroba
in thread Traversing a complex data structure searching for certain keys and printing their values by newperluser2013

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
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (3)
    As of 2018-05-21 23:06 GMT
    Find Nodes?
      Voting Booth?