Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: How do I parse values of a complex anonymous hash of hashes?

by Anonymous Monk
on May 07, 2001 at 18:55 UTC ( #78515=note: print w/replies, xml ) Need Help??

in reply to How do I parse values of a complex anonymous hash of hashes?

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://78515]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2018-06-21 05:23 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.