CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
substring problem

 



sayal
Novice

Apr 19, 2011, 5:41 AM

Post #1 of 3 (497 views)
substring problem Can't Post

Hi,

i want to get a substring of a string. I have tried it like this after searching in the weg, but it does not work:



my %write_row;

my $value = "uniprotkb:Q96KR1|intact:EBI-2513582";

$write_row{1} = $value =~ /uniprotkb\:(.+?)\|/;



As a result i get "1". I want to get the "Q96KR1";


(This post was edited by sayal on Apr 19, 2011, 7:06 AM)


Zhris
Enthusiast

Apr 19, 2011, 12:26 PM

Post #2 of 3 (492 views)
Re: [sayal] substring problem [In reply to] Can't Post

Hi,

The key thing to remember is that an array in scalar context will return the number of elements in the array, in your case "1". You will need to put parenthesis round "$write_row{1}", or use an array instead:


Code
($write_row{1}) = $value =~ /uniprotkb\:(.+?)\|/;  

#or

my @matches = $value =~ /uniprotkb\:(.+?)\|/;
my $firstmatch = $matches[0];


A regex returns the matches as an array, not as a scalar. There could potentially be more than 1 match.

Chris


(This post was edited by Zhris on Apr 19, 2011, 12:28 PM)


miller
User

Apr 19, 2011, 6:04 PM

Post #3 of 3 (483 views)
Re: [sayal] substring problem [In reply to] Can't Post

Zhris already recommended one right way to do it. However, you should consider adding error checking whenever you're matching against a regex like this:


Code
if ($value =~ /uniprotkb\:(.+?)\|/) { 
$write_row{1} = $1;
} else {
warn "regex did not match\n";
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives