By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,847 Members | 1,375 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,847 IT Pros & Developers. It's quick & easy.

Retrieving items from an array of hashes

P: 2
Hello! Hope someone can help! I'm having a nightmare navigating an array of hashes. I'm using the Perl Twitter Module - http://search.cpan.org/~cthom/Net-Tw...Net/Twitter.pm . Running this script...

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use Net::Twitter;
  4. use Data::Dumper;
  5.  
  6. print "Content-type: text/plain\n\n"; 
  7.  
  8. my $twit = Net::Twitter->new(username=>"username", password=>"password" );
  9. $result = $twit->replies();
  10.  
  11. print Dumper($result);
I get:
Expand|Select|Wrap|Line Numbers
  1. $VAR1 = [
  2.           {
  3.             'source' => 'web',
  4.             'truncated' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' ),
  5.             'created_at' => 'Thu Feb 21 02:27:32 +0000 2008',
  6.             'text' => 'Test Text',
  7.             'user' => {
  8.                         'location' => undef,
  9.                         'profile_image_url' => 'http://static.twitter.com/images/default_profile_normal.png',
  10.                         'protected' => $VAR1->[0]{'truncated'},
  11.                         'name' => 'myname',
  12.                         'description' => undef,
  13.                         'url' => undef,
  14.                         'id' => 123456,
  15.                         'screen_name' => 'myscreenname'
  16.                       },
  17.             'id' => 123123123
  18.           },
  19.           {
  20.             'source' => 'web',
  21.             'truncated' => $VAR1->[0]{'truncated'},
  22.             'created_at' => 'Thu Feb 21 01:18:55 +0000 2008',
  23.             'text' => 'Test Text',
  24.             'user' => {
  25.                         'location' => undef,
  26.                         'profile_image_url' => 'http://static.twitter.com/images/default_profile_normal.png',
  27.                         'protected' => $VAR1->[0]{'truncated'},
  28.                         'name' => 'myname',
  29.                         'description' => undef,
  30.                         'url' => undef,
  31.                         'id' => 123456,
  32.                         'screen_name' => 'myscreenname'
  33.                       },
  34.             'id' => 123123123
  35.           },
  36.         ]
I have no idea how to parse this, I can't even pull any items out. Should by $result even be a scalar? Please please help!

Really appreciate it,
Stuart
Feb 27 '08 #1
Share this Question
Share on Google+
4 Replies


KevinADC
Expert 2.5K+
P: 4,059
Do this, instead of:

print Dumper($result);

try this:

print Dumper(\$result);

and repost what gets printed. Sometimnes its a little hard to tell what the underlying data structure is if you do not pass a reference to the Dumper() function. But it does look like a hash of hash references.
Feb 27 '08 #2

P: 2
Thanks for this! Really appreciate it. Here you go:
Expand|Select|Wrap|Line Numbers
  1. $VAR1 = \[
  2.             {
  3.               'source' => 'web',
  4.               'truncated' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' ),
  5.               'created_at' => 'Thu Feb 21 01:27:32 +0000 2008',
  6.               'text' => 'Test Text',
  7.               'user' => {
  8.                           'location' => undef,
  9.                           'profile_image_url' => 'http://static.twitter.com/images/default_profile_normal.png',
  10.                           'protected' => ${$VAR1}->[0]->{'truncated'},
  11.                           'name' => 'myname',
  12.                           'description' => undef,
  13.                           'url' => undef,
  14.                           'id' => 123456,
  15.                           'screen_name' => 'myscreenname'
  16.                         },
  17.               'id' => 123123123
  18.             },
  19.             {
  20.               'source' => 'web',
  21.               'truncated' => ${$VAR1}->[0]->{'truncated'},
  22.               'created_at' => 'Thu Feb 21 00:18:55 +0000 2008',
  23.               'text' => 'Test Text',
  24.               'user' => {
  25.                           'location' => undef,
  26.                           'profile_image_url' => 'http://static.twitter.com/images/default_profile_normal.png',
  27.                           'protected' => ${$VAR1}->[0]->{'truncated'},
  28.                           'name' => 'myname',
  29.                           'description' => undef,
  30.                           'url' => undef,
  31.                           'id' => 123456,
  32.                           'screen_name' => 'myscreenname'
  33.                         },
  34.               'id' => 123123123
  35.             }
  36.         ];
Feb 27 '08 #3

Kelicula
Expert 100+
P: 176
Thanks for this! Really appreciate it. Here you go:
Expand|Select|Wrap|Line Numbers
  1. $VAR1 = \[
  2.             {
  3.               'source' => 'web',
  4.               'truncated' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' ),
  5.               'created_at' => 'Thu Feb 21 01:27:32 +0000 2008',
  6.               'text' => 'Test Text',
  7.               'user' => {
  8.                           'location' => undef,
  9.                           'profile_image_url' => 'http://static.twitter.com/images/default_profile_normal.png',
  10.                           'protected' => ${$VAR1}->[0]->{'truncated'},
  11.                           'name' => 'myname',
  12.                           'description' => undef,
  13.                           'url' => undef,
  14.                           'id' => 123456,
  15.                           'screen_name' => 'myscreenname'
  16.                         },
  17.               'id' => 123123123
  18.             },
  19.             {
  20.               'source' => 'web',
  21.               'truncated' => ${$VAR1}->[0]->{'truncated'},
  22.               'created_at' => 'Thu Feb 21 00:18:55 +0000 2008',
  23.               'text' => 'Test Text',
  24.               'user' => {
  25.                           'location' => undef,
  26.                           'profile_image_url' => 'http://static.twitter.com/images/default_profile_normal.png',
  27.                           'protected' => ${$VAR1}->[0]->{'truncated'},
  28.                           'name' => 'myname',
  29.                           'description' => undef,
  30.                           'url' => undef,
  31.                           'id' => 123456,
  32.                           'screen_name' => 'myscreenname'
  33.                         },
  34.               'id' => 123123123
  35.             }
  36.         ];
As far as I can see $VAR1 is an anonymous array, holding nested hash references with it.

As you can see this whole thing is deeply tied into references.
I recommend reading this Refernces
and This: Better Tutorial

Say for instance you wanted to get at the "id" field within the first element of the $VAR1 array;

$VAR1->[0]->{id};

123123123.

Or the "user" within the first hash within the first array element;

$VAR->[0]->{user}->{id};

123456.


This will print out everything but the blessed object references. (And thier references) :)



Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. for(@$VAR1){   # For each element of the array ref $VAR1
  4.     if(ref($_) eq 'HASH'){    # if that element is a hash ref
  5.         for my $q (sort keys %$_){   # for each element of that hash
  6.                     if(ref($$_{$q}) eq 'HASH'){   # if that element is a hash
  7.                         print $q.":\t";     # print the key
  8.                 for my $s (sort keys %{$$_{$q}}){  # for each element of that hash
  9.                       print "$s : ${$$_{$q}}{$s}\n\t";   # print the key and value
  10.                                 }
  11.                         }else{     # print the key values
  12.                 print "$q : $$_{$q}\n";
  13.                 } }
  14.         print "\n--------\n";   # print a separator
  15.     }
  16.         elsif(ref($_) eq 'ARRAY'){    # this doesn't happen, just put it in there for robustness
  17.         for(@$_){                     # if the current value of $_ was an array
  18.         print;
  19.         }
  20.         }
  21.         else{                         # if $_ was a scalar print it too..
  22.         print;
  23.         }
  24. }
  25.  
  26.  
The above code will output:
Expand|Select|Wrap|Line Numbers
  1. created_at : Thu Feb 21 01:27:32 +0000 2008
  2. id : 123123123
  3. source : web
  4. text : Test Text
  5. truncated : JSON::XS::Boolean=SCALAR(0x3603c)
  6. user:    description : 
  7.     id : 123456
  8.     location : 
  9.     name : myname
  10.     profile_image_url : http://static.twitter.com/images/default_profile_normal.png
  11.     protected : 
  12.     screen_name : myscreenname
  13.     url : 
  14.  
  15. --------
  16. created_at : Thu Feb 21 00:18:55 +0000 2008
  17. id : 123123123
  18. source : web
  19. text : Test Text
  20. truncated : 
  21. user:    description : 
  22.     id : 123456
  23.     location : 
  24.     name : myname
  25.     profile_image_url : http://static.twitter.com/images/default_profile_normal.png
  26.     protected : 
  27.     screen_name : myscreenname
  28.     url : 
  29.  
  30. --------
  31.  
As you can see there are still some values that need accessing.
But I had NO luck getting at the '0' value of the blessed object $o;
Or the pointer to it "protected".

Man there are enough references in this to make anyones head spin.
What are you trying to accomplish?

Maybe there's another way to do it.

After all, TMTOWTDI.
Feb 28 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
Just concurring, looks like an array of anonymous hashes (or nested hashes as Kelicula said). The links Kelicula posted should help you as well as what he posted.
Feb 28 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.