proctree
proctree

Reputation: 85

Not a HASH reference error received while trying to loop through array inside a hash

I've been trying to make a reddit bot which scrapes the moderator list from a given array of subreddits. So far I've only tried to use the reddit API to retrieve one single mod list (in JSON format). I've decoded it using the JSON perl module and now I'm trying to loop through it to build a mod list. Here's my code:

use strict;
use warnings;
use LWP;
use JSON;
use Data::Dumper;

my $ua = LWP::UserAgent->new('RedditBot/ModScraper');
my $req = HTTP::Request->new(GET => 'http://www.reddit.com/r/funny/about/moderators.json');
my $res = $ua->request($req);


if($res->is_success){
    my $djson = decode_json($res->content);
    my @datalist = $djson->{'data'}{'children'};
    my @names;
    for(my $i=0;$i<@datalist;$i++){
            push(@names, $datalist[$i]->{'name'});
    }
    print Dumper @names;

}
else{
    print $res->status_line, "\n";
}

And here is the Dumped hash/array:

$VAR1 = [
      {
        'mod_permissions' => [
                               'all'
                             ],
        'date' => '1201242956',
        'name' => 'illuminatedwax',
        'id' => 't2_14nvj'
      },
      {
        'mod_permissions' => [
                               'all'
                             ],
        'date' => '1282272786',
        'name' => 'krispykrackers',
        'id' => 't2_35gvu'
      },
      {
        'mod_permissions' => [
                               'all'
                             ],
        'date' => '1306697593',
        'name' => 'doug3465',
        'id' => 't2_4d9s0'
      },
      {
        'mod_permissions' => [
                               'all'
                             ],
        'date' => '1306884548',
        'name' => 'Kylde',
        'id' => 't2_q22x'
      },
      {
        'mod_permissions' => [
                               'all'
                             ],
        'date' => '1318239301',
        'name' => 'GuitarFreak027',
        'id' => 't2_3uh1q'
      },
      {
        'mod_permissions' => [
                               'all'
                             ],
        'date' => '1328330491',
        'name' => 'sodypop',
        'id' => 't2_39hzo'
      },
      {
        'mod_permissions' => [
                               'all'
                             ],
        'date' => '1334668866',
        'name' => 'brownboy13',
        'id' => 't2_4itnj'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'access',
                               'mail',
                               'flair'
                             ],
        'date' => '1338375544',
        'name' => 'neptath',
        'id' => 't2_42sls'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'access',
                               'mail',
                               'flair'
                             ],
        'date' => '1338375575',
        'name' => 'kabuliwallah',
        'id' => 't2_3wqp9'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'access',
                               'mail',
                               'flair'
                             ],
        'date' => '1338375584',
        'name' => 'poop_monster',
        'id' => 't2_45a14'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'access',
                               'mail',
                               'flair'
                             ],
        'date' => '1342052024',
        'name' => 'WellEndowedMod',
        'id' => 't2_66grn'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'access',
                               'mail',
                               'flair'
                             ],
        'date' => '1351424249',
        'name' => 'complex_reduction',
        'id' => 't2_4y36s'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'access',
                               'mail',
                               'flair'
                             ],
        'date' => '1351427857',
        'name' => 'anions',
        'id' => 't2_33ggu'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'access',
                               'mail',
                               'flair'
                             ],
        'date' => '1363446597',
        'name' => 'Livyka',
        'id' => 't2_5p6ae'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'access',
                               'mail',
                               'flair'
                             ],
        'date' => '1363450026',
        'name' => 'Lord_Nuke',
        'id' => 't2_4fvxr'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'access',
                               'mail',
                               'flair'
                             ],
        'date' => '1363450241',
        'name' => 'redtaboo',
        'id' => 't2_3belm'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'access',
                               'mail',
                               'flair'
                             ],
        'date' => '1363453699',
        'name' => 'Knowltey',
        'id' => 't2_3aadp'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'flair'
                             ],
        'date' => '1401789143',
        'name' => 'AutoModerator',
        'id' => 't2_6l4z3'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'access',
                               'mail',
                               'flair'
                             ],
        'date' => '1401789148',
        'name' => 'thejellydude',
        'id' => 't2_3g59f'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'access',
                               'mail',
                               'config',
                               'flair'
                             ],
        'date' => '1401789172',
        'name' => 'DuckDragon',
        'id' => 't2_52snu'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'access',
                               'mail',
                               'flair'
                             ],
        'date' => '1401789188',
        'name' => 'Kensin',
        'id' => 't2_4nbrl'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'access',
                               'mail',
                               'flair'
                             ],
        'date' => '1401789200',
        'name' => 'cahman',
        'id' => 't2_6y7ld'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'access',
                               'mail',
                               'flair'
                             ],
        'date' => '1401789214',
        'name' => 'topodan',
        'id' => 't2_8nisz'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'access',
                               'mail',
                               'flair'
                             ],
        'date' => '1401789225',
        'name' => 'ADHD_orc',
        'id' => 't2_5dwik'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'access',
                               'mail',
                               'flair'
                             ],
        'date' => '1401789236',
        'name' => 'all_my_rage',
        'id' => 't2_6z81j'
      },
      {
        'mod_permissions' => [
                               'mail'
                             ],
        'date' => '1401789251',
        'name' => 'funny_mod',
        'id' => 't2_5qz0k'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'mail',
                               'flair'
                             ],
        'date' => '1401789269',
        'name' => 'benologist',
        'id' => 't2_358ts'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'access',
                               'mail',
                               'flair'
                             ],
        'date' => '1401789281',
        'name' => 'dizzie131',
        'id' => 't2_bb3q5'
      },
      {
        'mod_permissions' => [
                               'wiki',
                               'posts',
                               'access',
                               'mail',
                               'flair'
                             ],
        'date' => '1401789298',
        'name' => 'KimJongUnchained',
        'id' => 't2_b9c4n'
      }
    ];

My error is:

Not a HASH reference at modscraper.pl line 15.

I know this is probably something trivial, but I'm a pretty new Perl programmer and haven't done much JSON/Networking work before. Thanks for reading.

EDIT: Forgot to mention, line 15 is the for loop and the dumped array/hash is @datalist.

Upvotes: 3

Views: 3390

Answers (1)

anurag
anurag

Reputation: 202

As the dumped hash/array value that you have given is a array reference, so the datalist variable should be defined as a array ref, so the code line for that should be changed to:

my $datalist = $djson->{'data'}{'children'};

also, we have to change the for loop code because now datalist is a array ref, so you can change the for loop code to this:

for(my $i=0;$i<@$datalist;$i++){
    push(@names, $datalist->[$i]->{'name'});
}

Upvotes: 2

Related Questions