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: Regular Expressions:
storing matches

 



PGScooter
stranger

Apr 20, 2007, 1:13 PM

Post #1 of 6 (4475 views)
storing matches Can't Post

Hi, I have a few questions about storing matches found with regexes. The "error" I get is that I get the number of results not returned, not the results. I think my problem has to do with scalars, but I'm not sure how to switch modes per se.

The following piece of code works, but I am curious why the ones below do not.


Code
  

@pronunciation=($definition=~/Pronunciation\[*\]/g);



How can I store just the first match? The following do not work:


Code
@pronunciation=($definition=~/Pronunciation\[*\]/);  



$pronunciation=($definition=~/Pronunciation\[*\]/);



How can I substitute and store what was substituted? The following does not work:


Code
@pronunciation=($definition=~s/Pronunciation\[*\]/OUT/g);



Thank you!

Scott

The more you teach me, the more I learn. The more I learn, the more I teach.


osmodius
Novice

Apr 21, 2007, 4:02 PM

Post #2 of 6 (4466 views)
Re: [PGScooter] storing matches [In reply to] Can't Post

Well, in response to your first question - why do they not work? It's because in a scalar context, the match operator (m//) will return true or false, and NOT the string that triggered the match. The following code will work,


Code
if($definition =~ m/(Pronunciation\[*\])/){  
$pronunciation = $1;
}


And for the second question - why does it not work? Because the substitute operator (s///) returns the number of strings substituted, and not the strings that were substituted. To get around this, you can do the following:


Code
while($definition =~ s/(Pronunciation\[*\])/OUT/g){ 
push(@pronunciation, $1);
}



Need some real-time help? Shove admin@ub3r.net on yer MSN.


PGScooter
stranger

Apr 21, 2007, 9:58 PM

Post #3 of 6 (4463 views)
Re: [osmodius] storing matches [In reply to] Can't Post

Osmodius,

Thank you for the help and the explanation. Although I still don't quite understand why the one I posted that does work is not in scalar context, that clears up some things.

Thanks,

Scott
The more you teach me, the more I learn. The more I learn, the more I teach.


osmodius
Novice

Apr 22, 2007, 4:04 AM

Post #4 of 6 (4460 views)
Re: [PGScooter] storing matches [In reply to] Can't Post

In response to your further question - why is it not in scalar context? Because you are assigning it to an array. Therefore, the assignment operator is waiting on a rvalue with a list context. And bingo, it gets one! The match operator (m//) returns the list of strings matched WHEN used in conjunction with the /g modifier.

Hope that clears things up for you.


Need some real-time help? Shove admin@ub3r.net on yer MSN.


PGScooter
stranger

Apr 22, 2007, 3:29 PM

Post #5 of 6 (4454 views)
Re: [osmodius] storing matches [In reply to] Can't Post

Yes, osmodius, that does help, I just have to contemplate this stuff a little bit and let it sink it :)

Thanks
The more you teach me, the more I learn. The more I learn, the more I teach.


KevinR
Veteran


Apr 22, 2007, 7:47 PM

Post #6 of 6 (4451 views)
Re: [PGScooter] storing matches [In reply to] Can't Post

Some of the stuff here may help although it's not a thorough regexp tutorial:

http://perldoc.perl.org/perlrequick.html
-------------------------------------------------

 
 


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

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