-1

So I found these links which are related for the task. First using python, second using c#, third using Perl

Now I'm too new with Perl and what I want to do is work with some json streams from twitter. What I'm looking at is this:

..E","location":"Hollywood, Los Angeles, CA ","screen_name":"i..

How do I find "location": using regex and then assign a variable to contain Hollywood, Los Angeles, CA?

sub get_location {
# pseudo code:  
# look for "location":"xxxxxxxxxxxxxxxx" 
# assign $tmp_loc = Hollywood, Los Angeles, CA (in this case)
# return $tmp_loc; }
Community
  • 1
  • 1
oalmgren
  • 415
  • 7
  • 19

4 Answers4

8

Perl has libraries for dealing with JSON. Why not use one of those?

Alternatively, as you're dealing with Twitter, why not use Net::Twitter which makes Twitter API calls and returns the results to you as Perl data structures.

These days, a lot of Perl programming is a matter of knowing which CPAN modules to string together. If you're not using CPAN, then you're missing out on a lot of the power of Perl.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
3

Perl JSON.

Halil Özgür
  • 15,731
  • 6
  • 49
  • 56
3

You really shouldn't use regular expressions anywhere you need to find something in a string... They have their purpose, but not here.

If you have a JSON encoded string just decode it. I have no experience with Perl but I see that others recommend using a module from CPAN.

Alin Purcaru
  • 43,655
  • 12
  • 77
  • 90
0

First you need a regex that matches the text you want to capture. Since you only put a snippet I will only put a snippet of it also.

$text = ' ..E","location":"Hollywood, Los Angeles, CA ","screen_name":"i..';
if( $text =~ /.*location":"(.[^"]+)",.*/ ) {
  $tmp_loc = $1;
}
return $tmp_loc;
krico
  • 5,723
  • 2
  • 25
  • 28
  • The recommendation by others to use the CPAN module is the right answer, at least to the question we all think *should* have been asked. That said, the text above is the precise answer to the question and is very handy to have. Both answers are valuable. – zerolagtime Nov 08 '10 at 16:58
  • Yes I agree, thank you krico and the others who took their time answering. – oalmgren Nov 09 '10 at 01:09