Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Not an array reference Twitter API

by neelakash21 (Initiate)
on Sep 12, 2012 at 21:01 UTC ( #993315=perlquestion: print w/ replies, xml ) Need Help??
neelakash21 has asked for the wisdom of the Perl Monks concerning the following question:

use Net::Twitter; use Scalar::Util 'blessed'; use Net::Twitter::Search; # When no authentication is required: my $nt = Net::Twitter->new(legacy => 0); $consumer_key = "x"; $consumer_secret = "y"; $token = "z"; $token_secret = "w"; # As of 13-Aug-2010, Twitter requires OAuth for authenticated requests my $nt = Net::Twitter->new( traits => [qw/OAuth API::REST/], consumer_key => $consumer_key, consumer_secret => $consumer_secret, access_token => $token, access_token_secret => $token_secret, ); my $twitter = Net::Twitter::Search->new(); my $results = $twitter->search('smoking'); foreach my $tweet (@{$results}) { my $speaker = $tweet->{from_user}; my $text = $tweet->{text}; my $time = $tweet->{created_at}; print "$time <$speaker> $text\n"; } }

I am new to perl. I am getting an error "Not an ARRAY reference at line #" for the above code. Can anyone please help me with this problem?

Comment on Not an array reference Twitter API
Download Code
Re: Not an array reference Twitter API
by tobyink (Abbot) on Sep 12, 2012 at 21:14 UTC

    $results isn't a reference to an array, so you can't loop through it. According to the Net::Twitter documentation, it's a reference to a hash. This hash has a key "results", the value of which is an array ref, so you can loop through it.

    You probably want something like...

    foreach my $tweet ( @{$results->{results}} ) { printf( "%s <%s> %s\n", $tweet->{created_at}, $tweet->{from_user}, $tweet->{text}, ); }
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      Thanks for pointing it out. Your code works perfectly.
Re: Not an array reference Twitter API
by Voronich (Hermit) on Sep 13, 2012 at 14:32 UTC
    As an aside, you're going to want to edit that post so that your key information isn't showing. I'd recommend replacing them with Xs or something.
      Thanks! I don't know how I forgot to mask that information! I have fixed it now.
        hi this code return to me 15 tweets on one day by hashtag i want to have all tweets by week pleaase help me

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2014-11-26 07:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (164 votes), past polls