Retrieving items from an array of hashes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tobhut
    New Member
    • Feb 2008
    • 2

    #1

    Retrieving items from an array of hashes

    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...

    [CODE=perl]#!/usr/bin/perl

    use Net::Twitter;
    use Data::Dumper;

    print "Content-type: text/plain\n\n";

    my $twit = Net::Twitter->new(username=> "username", password=>"pass word" );
    $result = $twit->replies();

    print Dumper($result) ;[/CODE]

    I get:
    Code:
    $VAR1 = [
              {
                'source' => 'web',
                'truncated' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' ),
                'created_at' => 'Thu Feb 21 02:27:32 +0000 2008',
                'text' => 'Test Text',
                'user' => {
                            'location' => undef,
                            'profile_image_url' => 'http://static.twitter.com/images/default_profile_normal.png',
                            'protected' => $VAR1->[0]{'truncated'},
                            'name' => 'myname',
                            'description' => undef,
                            'url' => undef,
                            'id' => 123456,
                            'screen_name' => 'myscreenname'
                          },
                'id' => 123123123
              },
              {
                'source' => 'web',
                'truncated' => $VAR1->[0]{'truncated'},
                'created_at' => 'Thu Feb 21 01:18:55 +0000 2008',
                'text' => 'Test Text',
                'user' => {
                            'location' => undef,
                            'profile_image_url' => 'http://static.twitter.com/images/default_profile_normal.png',
                            'protected' => $VAR1->[0]{'truncated'},
                            'name' => 'myname',
                            'description' => undef,
                            'url' => undef,
                            'id' => 123456,
                            'screen_name' => 'myscreenname'
                          },
                'id' => 123123123
              },
    		]
    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
    Last edited by eWish; Feb 27 '08, 11:38 PM. Reason: Please use [CODE][/CODE]tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    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.

    Comment

    • tobhut
      New Member
      • Feb 2008
      • 2

      #3
      Thanks for this! Really appreciate it. Here you go:
      Code:
      $VAR1 = \[
                  {
                    'source' => 'web',
                    'truncated' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' ),
                    'created_at' => 'Thu Feb 21 01:27:32 +0000 2008',
                    'text' => 'Test Text',
                    'user' => {
                                'location' => undef,
                                'profile_image_url' => 'http://static.twitter.com/images/default_profile_normal.png',
                                'protected' => ${$VAR1}->[0]->{'truncated'},
                                'name' => 'myname',
                                'description' => undef,
                                'url' => undef,
                                'id' => 123456,
                                'screen_name' => 'myscreenname'
                              },
                    'id' => 123123123
                  },
                  {
                    'source' => 'web',
                    'truncated' => ${$VAR1}->[0]->{'truncated'},
                    'created_at' => 'Thu Feb 21 00:18:55 +0000 2008',
                    'text' => 'Test Text',
                    'user' => {
                                'location' => undef,
                                'profile_image_url' => 'http://static.twitter.com/images/default_profile_normal.png',
                                'protected' => ${$VAR1}->[0]->{'truncated'},
                                'name' => 'myname',
                                'description' => undef,
                                'url' => undef,
                                'id' => 123456,
                                'screen_name' => 'myscreenname'
                              },
                    'id' => 123123123
                  }
      		];
      Last edited by eWish; Feb 27 '08, 11:39 PM. Reason: Please use [CODE][/CODE]tags

      Comment

      • Kelicula
        Recognized Expert New Member
        • Jul 2007
        • 176

        #4
        Originally posted by tobhut
        Thanks for this! Really appreciate it. Here you go:
        Code:
        $VAR1 = \[
                    {
                      'source' => 'web',
                      'truncated' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' ),
                      'created_at' => 'Thu Feb 21 01:27:32 +0000 2008',
                      'text' => 'Test Text',
                      'user' => {
                                  'location' => undef,
                                  'profile_image_url' => 'http://static.twitter.com/images/default_profile_normal.png',
                                  'protected' => ${$VAR1}->[0]->{'truncated'},
                                  'name' => 'myname',
                                  'description' => undef,
                                  'url' => undef,
                                  'id' => 123456,
                                  'screen_name' => 'myscreenname'
                                },
                      'id' => 123123123
                    },
                    {
                      'source' => 'web',
                      'truncated' => ${$VAR1}->[0]->{'truncated'},
                      'created_at' => 'Thu Feb 21 00:18:55 +0000 2008',
                      'text' => 'Test Text',
                      'user' => {
                                  'location' => undef,
                                  'profile_image_url' => 'http://static.twitter.com/images/default_profile_normal.png',
                                  'protected' => ${$VAR1}->[0]->{'truncated'},
                                  'name' => 'myname',
                                  'description' => undef,
                                  'url' => undef,
                                  'id' => 123456,
                                  'screen_name' => 'myscreenname'
                                },
                      'id' => 123123123
                    }
        		];
        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) :)



        [code=perl]
        use strict;

        for(@$VAR1){ # For each element of the array ref $VAR1
        if(ref($_) eq 'HASH'){ # if that element is a hash ref
        for my $q (sort keys %$_){ # for each element of that hash
        if(ref($$_{$q}) eq 'HASH'){ # if that element is a hash
        print $q.":\t"; # print the key
        for my $s (sort keys %{$$_{$q}}){ # for each element of that hash
        print "$s : ${$$_{$q}}{$s}\ n\t"; # print the key and value
        }
        }else{ # print the key values
        print "$q : $$_{$q}\n";
        } }
        print "\n--------\n"; # print a separator
        }
        elsif(ref($_) eq 'ARRAY'){ # this doesn't happen, just put it in there for robustness
        for(@$_){ # if the current value of $_ was an array
        print;
        }
        }
        else{ # if $_ was a scalar print it too..
        print;
        }
        }

        [/code]
        The above code will output:
        [code=text]
        created_at : Thu Feb 21 01:27:32 +0000 2008
        id : 123123123
        source : web
        text : Test Text
        truncated : JSON::XS::Boole an=SCALAR(0x360 3c)
        user: description :
        id : 123456
        location :
        name : myname
        profile_image_u rl : http://static.twitter. com/images/default_profile _normal.png
        protected :
        screen_name : myscreenname
        url :

        --------
        created_at : Thu Feb 21 00:18:55 +0000 2008
        id : 123123123
        source : web
        text : Test Text
        truncated :
        user: description :
        id : 123456
        location :
        name : myname
        profile_image_u rl : http://static.twitter. com/images/default_profile _normal.png
        protected :
        screen_name : myscreenname
        url :

        --------
        [/code]

        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.

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          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.

          Comment

          Working...