Hi Perl monks I have a problem that I can't seem to get working and I was hoping you could help me please. I have a subrountine which I am using to parse a complex data structure and as I go through all the nested hashes I am looking for a list of certain keys and once I have all of these keys I am printing a line of output for each time I find a full set of keys. I am storing the list of keys in @keys_to_find and this looks like this:

%volume %mnt %device %perms %user %mntops %other

The subroutine I am using to parse the complex data structure and pass in @keys_to_find looks like this. This is able to correctly parse the data structure and print out individual lines containing all the values of each one of the keys in @keys_to_find.

sub traverse_hash { my ($jsonhash, $found, @keys_to_find) = @_; my $results = 'results.txt'; open(FILE, ">>$results"); for my $key (sort keys %{$jsonhash}) { my $keyvalue = $jsonhash->{$key}; if ('HASH' eq ref $keyvalue) { $found = {%$found, %{ traverse_hash($keyvalue, $found, @ke +ys_to_find) }}; } for my $find (@keys_to_find) { if ($key eq $find) { if ('HASH' eq ref $keyvalue) { $found->{$key} = [ map $keyvalue->{$_}, sort keys +%$keyvalue ]; } else { $found->{$key} = $keyvalue; } } } } if (@keys_to_find == keys %$found) { my $count; $count = @{ (grep 'ARRAY' eq ref $_, values %$found)[0] // [] +}; $count ||= 1; for my $i (0 .. $count - 1) { print FILE join(':', map { 'ARRAY' eq ref $_ ? $_->[$i] : +$_ } @{$found}{@keys_to_find}), "\n"; } $found = {}; } return $found; close(FILE); }

What I am struggling to work out how to do is to additionally pass into this subroutine an output format string so I can print the results in different formats rather than print lines containing every value for all the keys in @keys_to_find. So for example I want to pass in a a variable which contains an output format string like so:

$output_format = '%mnt:%device:%volume:%perms';

So with this $output_format string I want to be able to call the subroutine like this:

traverse_hash(\%$decoded_json_obj, {}, @keys_to_find, $output_format);

So when this subroutine is called like above I want it first to make sure that every key in @keys_to_find exists at each level in data structure (like it currently does) and then if all those keys exist then print out a line/s of output in the format of whatever keys are defined in $output_format string variable. The contents of $output_format will be different each time I call this subroutine so I can't pass into this subroutine a hardcoded string. I need to pass this in as a variable. Please can you help me with how to do this?


In reply to How to print a string in a specific format when parsing a complex data structure by newperluser2013

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.