-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; }
0

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

3

Perl JSON.

Comments

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.

Comments

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;

2 Comments

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.
Yes I agree, thank you krico and the others who took their time answering.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.