Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

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

It's very likely that i'm misunderstanding this, but let's see. It sounds like you want to deanonymise a hash of hashes, ie to create a named hash with the exact same data structure? i'm not sure why you would need to do that, but i've been fiddling with some examples and i can't see any reason for not just using:

my %bar = %$foo;

On the other hand, the cookbook says that you should use dclone for this sort of thing, which is in a CPAN module called Storable. I think the difference is that dclone will create a whole new structure whereas the assignment above just creates an identity between your named hash and the anonymous one, so they still use the same references. There might be reasons why that's undesirable, but i'm definitely out of my depth there. Perhaps when you want to amend values in the named hash but not affect the original?

But you also say that you want to parse the data, by which i suppose you mean that you want each key/value pair to pass through your hands so that you can decide whether or not it's important. That seems like a separate problem to me, and the key is probably a test for hashness: the rest is pretty simple and the way to go about it depends on your data. If you know that it's only there are no arrays in the HoH, then you might be able to get away with using ref($foo{$_}), which will return 'HASH' if $foo{$_} is a hash and FALSE if it isn't a reference at all. I also remember seeing something that just used a regex to check whether the string 'HASH' was in the value, but that sounds highly dubious to me. Anyway, given such a test, it would be relatively simple to step through every pair and look for whatever you're looking for.

But if you're really thinking of stepping through the HoH and testing for particular name-value pairs, or something like that, then I'd say you should be looking for a different approach rather than dealing with this problem as it stands: there's no point parsing xml line by line, which is essentially what you'd be doing. At the very least, you should be able to simplify your code by knowing whether the important data corresponds to the first H of HoH or the second (or third or whatever). Otherwise you lose all the advantages of using xml in the first place. Also i've tried to make up a few ways of doing that - much more fun that what i'm supposed to be doing - and none of them have worked :(

Hope this helps.


In reply to Re: How do I parse values of a complex anonymous hash of hashes? by Anonymous Monk
in thread How do I parse values of a complex anonymous hash of hashes? by Anonymous Monk

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 chilling in the Monastery: (2)
As of 2024-04-26 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found