Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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?

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://993315]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2018-05-25 07:52 GMT
Find Nodes?
    Voting Booth?