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

I'm using the twittercounter API to try and graph my twitter stats from the XML file. Once I get the XML file I use the data structure returned from XML::Simple but can't seem to access the hash of hashes for followersperdat. Here is the hash snippet:
'growth_since_2w' => '16923', 'followersperdate' => { 'date2009-09-03' => '17839', 'date2009-08-14' => '17309', 'date2009-07-11' => '15530', 'date2009-06-23' => '13116', 'date2009-09-05' => '17883', 'date2009-08-25' => '17668', 'date2009-08-12' => '17309', 'date2009-09-09' => '17883', 'date2009-08-09' => '16754', 'date2009-08-10' => '16754', 'date2009-06-26' => '13845', 'date2009-08-27' => '17668', 'date2009-08-24' => '17668', 'date2009-06-29' => '14294', 'date2009-08-22' => '17668', 'date2009-08-11' => '16754', 'date2009-08-18' => '17309', 'date2009-08-02' => '16579', 'date2009-08-01' => '16579', 'date2009-09-08' => '17883', 'date2009-08-28' => '17668', 'date2009-09-06' => '17883', 'date2009-06-16' => '10161', 'date2009-08-20' => '17668', 'date2009-08-05' => '16579', 'date2009-09-04' => '17839', 'date2009-08-16' => '17309', 'date2009-08-03' => '16579', 'date2009-06-21' => '12101', 'date2009-08-21' => '17668', 'date2009-09-02' => '17839', 'date2009-09-10' => '17883', 'date2009-08-30' => '17830', 'date2009-08-26' => '17668', 'date2009-08-23' => '17668', 'date2009-07-31' => '16468', 'date2009-08-15' => '17309', 'date2009-08-19' => '17668', 'date2009-06-17' => '10161', 'date2009-08-17' => '17309', 'date2009-08-06' => '16579', 'date2009-08-04' => '16579', 'date2009-08-29' => '17830', 'date2009-06-25' => '13116', 'date2009-08-31' => '17830', 'date2009-07-14' => '15530', 'date2009-07-08' => '14685', 'date2009-06-18' => '10807', 'date2009-09-07' => '17883', 'date2009-07-02' => '14685', 'date2009-06-24' => '13116', 'date2009-07-18' => '15586', 'date2009-07-05' => '14685', 'date2009-09-11' => '17883', 'date2009-06-22' => '12552', 'date2009-08-13' => '17309', 'date2009-09-01' => '17839', 'date2009-06-19' => '11401', 'date2009-06-20' => '11401' }, 'tomorrow' => '18014',
Here is my data structure I'm using:
my $data = $xml->XMLin( $my_xml_file ); my %followers = $data->{followersperdate}; ...
basically I just need the contents of 'followersperdate' so I can sort the dates and then use the corresponding values to plot my graph. Any help would be greatly appreciated. Can someone point me in the right direction?

Replies are listed 'Best First'.
Re: access hash of hashes?
by ikegami (Patriarch) on Sep 11, 2009 at 17:39 UTC

    Hashes (such as %$data) can only contains scalars for values. So $data->{followersperdate} can't possibly hold a hash as you believe. Note the curlies in the Data::Dumper output. It holds a reference to a hash.

    my $followers = $data->{followersperdate}; for my $date ( keys %$followers ) { my $id = $followers->{$date}; ... }
      Thanks, that was it :)
Re: access hash of hashes?
by BioLion (Curate) on Sep 11, 2009 at 17:47 UTC

    Looks like you are not dereferencing the followers hash :

    my $followers = $data->{followersperdate}; ## just copy the reference +to the interesting part of the hash ** OR ** my %followers = %{ $data->{followersperdate} }; ## dereference the int +eresting bit

    See perlref for more details.

    Other than for clarity this doesn't make much sense though, because you alredy have the data in a not-too-complex HoH, but you might want to look at Tie::Hash::Sorted, or just create an array of the hash keys in sorted order :

    my @sorted_keys = sort { $data->{ followersperdate }->{ $b } <=> $data->{ followersperdate }->{ $a } } ## hi to lo on values keys %{ $data->{ followersperdate } }; ## again dereferencing as + above

    Or you can directly access the contents this way, if you only need the sorted contents the once.
    HTH.

    Just a something something...