Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

In addition to the issue of assigning default values in the cases where your input data provides none, you also have the issue that your input data has a variable organization. The value of the ProductVersion key might be a reference to an anonymous hash containing the details of a single product version or it might be a reference to an anonymous hash with a key for each of several product versions, where each key is the 'name' of the product version and each value is a reference to an anonymous hash containing the details of that product version (except for the name field, which is the key value).

OK, even I can't read that, and I like words. What I would do is normalize the structure, then process that, maybe like this:

foreach my $product (keys %sw) { print "Now processing $product\n"; my $version = '0'; my $release = '0'; my $description = $sw{$product}{'description'}; my $vendorUniqueKeyRef = $sw{$product}{'vendorUniqueKeyRef'}; my $swUniqueKey = $sw{$product}{'swUniqueKey'}; if ($sw{$product}{ProductVersion}) { if(exists($sw{$product}{ProductVersion}{name})) { # Push the immediate hash down, keyed by name # ignoring the duplicate name field $sw{$product}{ProductVersion} = { $sw{$product}{ProductVersion}{name} => $sw{$product}{P +roductVersion} }; } foreach my $product_version_name (keys %{$sw{$product}{Product +Version}}) { if($sw{$produt}{ProductVersion}{$product_version_name}{Pro +ductReleaseVersion}) { if(exists($sw{$produt}{ProductVersion}{$product_versio +n_name}{ProductReleaseVersion}{name})) { # Push the immediate hash down, keyed by name # ignoring the duplicate name field $sw{$produt}{ProductVersion}{$product_version_name +}{ProductReleaseVersion} = { $sw{$produt}{ProductVersion}{$product_version_ +name}{ProductReleaseVersion}{name} => $sw{$produt}{ProductVersion}{$product_vers +ion_name}{ProductReleaseVersion}; }; } foreach my $product_version_release_name (keys %{$sw{$ +product}{ProductVersion}{$product_version_name}{ProductReleaseVersion +}}) { # Print / store a record for the case that there i +s both a ProductVersion and ProductReleaseVersion } } else { # Print / store a record for the case that there is a +ProductVersion but no ProductReleaseVersion } } } else { # Print / store a record for the case that there is no Product +Version (and, therefore, no nested ProductReleaseVersion) } };

I haven't actually printed the output. I would probably do that with some subroutines to avoid duplication, and set defaults if values are missing.


In reply to Re: Iterative HOH or AOH? by ig
in thread Iterative HOH or AOH? by Smaug

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-05-26 17:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found