Hello Monks!

I have a data structure like so:

{ "/": { "type": "dir", "files": [ { "p": { "type": "dir-link", "source": "/nfs/data/project", "files": [ { "xa": { "type": "dir", "files": [ { "tools": { "type": "dir", "files": [ { "sd": { "type": "dir", "files": [ { "su2av": { "type": "dir", "files": [ { "duf": { "type": "dir", "files": [ { "0.4.0": { "type": "dir-link", "source": "/nfs/vsa/proj +ect/xa/vuvua/su2av", "files": [ { "bin": { "type": "dir", "files": [ { "duf": { "type": "lin +k-file", "source": ". +run" } }, { ".run": { "type": "fil +e" } } ] } } ] } } ] } } ] } } ] } } ] } } ] } } ] } }, { "nfs": { "type": "dir", "files": [ { "data": { "type": "dir", "files": [ { "project": { "type": "dir", "files": [] } } ] } }, { "vsa": { "type": "dir", "files": [ { "project": { "type": "dir", "files": [ { "xa": { "type": "dir", "files": [ { "vuvua": { "type": "dir", "files": [ { "su2av": { "type": "dir", "files": [] } } ] } } ] } } ] } } ] } } ] } } ] } }

It contains dirs, dir-links, files, file-links. I want to create a subroutine that returns an a array of directories, array of files and array on links. For example for directories I get:
/p/xa/tools/sd/su2av/duf/0.4.0/bin /nfs/data/project /nfs/vsa/project/xa/vuvua/su2av
For links it will be a bit different because I should have source and target so I will do this part myself. How can I get all the paths (dirs/files)? I thought of using recursion as I don't know how deep the nested hash is. How can I do it without grep? my code to test it:
my $j = '{ "/": { "type": "dir", "files": [ { "p": { "type": "dir-link +", "source": "/nfs/data/project", "files": [ { "xa": { "type": "dir", + "files": [ { "tools": { "type": "dir", "files": [ { "sd": { "type": +"dir", "files": [ { "su2av": { "type": "dir", "files": [ { "duf": { " +type": "dir", "files": [ { "0.4.0": { "type": "dir-link", "source": " +/nfs/vsa/project/xa/vuvua/su2av", "files": [ { "bin": { "type": "dir" +, "files": [ { "duf": { "type": "link-file", "source": ".run" } }, { +".run": { "type": "file" } } ] } } ] } } ] } } ] } } ] } } ] } } ] } +} ] } }, { "nfs": { "type": "dir", "files": [ { "data": { "type": "di +r", "files": [ { "project": { "type": "dir", "files": [] } } ] } }, { + "vsa": { "type": "dir", "files": [ { "project": { "type": "dir", "fi +les": [ { "xa": { "type": "dir", "files": [ { "vuvua": { "type": "dir +", "files": [ { "su2av": { "type": "dir", "files": [] } } ] } } ] } } + ] } } ] } } ] } } ] } }'; my $obj = decode_json($j);

In reply to How to iterate over nested hash and get all paths? by ovedpo15

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.