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

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

by Anonymous Monk
on May 07, 2001 at 11:53 UTC ( #78448=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've got an anonymous hash $foo from XML::Simple, and now I need to parse every value of this hash, but I've got no idea how to do it. Help?
  • Comment on How do I parse values of a complex anonymous hash of hashes?

Replies are listed 'Best First'.
Re: How do I parse values of a complex anonymous hash of hashes?
by epoptai (Curate) on May 07, 2001 at 15:00 UTC
    It's hard to answer this question without seeing the data structure, but here are a few clues.

    Use Data::Dumper to see what the hash looks like:

    use Data::Dumper; print header('text/plain'); print Dumper($foo);
    If you use forcearray when parsing the XML:
    $foo = XMLin($xml, forcearray => 1);
    Then you can access the data in a structure like this (which is exactly what Dumper prints):
    $foo = { 'INFO' => [ { 'lastchecked' => '20010506070758', 'content' => 'Rendered by the Newest Nodes XML Generator' +, 'site' => '', 'sitename' => 'Perl Monks' } ] };
    With loops like this:
    if(defined @{$data->{'INFO'}}){ for my $when(@{$data->{'INFO'}}){ $lastcheck = $when->{'lastchecked'} } }

    Super search around perlmonks a bit for more info.

Re: How do I parse values of a complex anonymous hash of hashes?
by Anonymous Monk on May 07, 2001 at 18:55 UTC

    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.

Re: How do I parse values of a complex anonymous hash of hashes?
by Anonymous Monk on May 07, 2001 at 15:19 UTC
    Thanks, epoptai,

    but this approach isn't quite what I'm looking for, since it is possible - I should even say probable - that the data structure has different key names from time to time. Therefore the real need is for a routine, that finds every key-value pair from an anonymous hash and after parsing the values return an exact copy of the original structure.

    And this is beyond my knowledge, sadly.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://78448]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2020-09-29 21:35 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (153 votes). Check out past polls.