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:
Matching a single character

 



kencl
User

Mar 27, 2000, 3:10 PM

Post #1 of 7 (1231 views)
Matching a single character Can't Post

What's wrong with this? It's not finding the single character ("g" in this example) in the string ("dgp" in this example).

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$NewListInitial = "g";
$CurrentListInitials = "dgp";
if (/$NewListInitial/g !~ $CurrentListInitials) { # THIS IS RETURNING FALSE!
$CurrentListInitials .= $NewListInitial; # Appends the new list initial
}
</pre><HR></BLOCKQUOTE>

Thanks.


kencl
User

Mar 27, 2000, 3:30 PM

Post #2 of 7 (1231 views)
Re: Matching a single character [In reply to] Can't Post

Correction, the expression SHOULD return false, but it's returning true and executing the statement, causing multple copies of "g" to end up in $CurrentListInitials


japhy
Enthusiast

Mar 27, 2000, 4:24 PM

Post #3 of 7 (1231 views)
Re: Matching a single character [In reply to] Can't Post

You have it backwards. Regular expressions are done as: EXPR =~ PATTERN (or EXPR !~ PATTERN). You're putting the pattern on the left side.


kencl
User

Mar 27, 2000, 10:59 PM

Post #4 of 7 (1231 views)
Re: Matching a single character [In reply to] Can't Post

Yes, thanks for the additional explanation. That's what I like about this forum. Not only can I get help with an immediate problem, but often contributors such as yourself explain why it was a problem in the first place. Anybody can cut, paste and tweak, but having someone explain some of PERL's less obvious workings helps everyone who reads the post become a better programmer.

In this case, I had to reverse every regular expression in my script! Now I understand why.


kencl
User

Mar 28, 2000, 8:15 AM

Post #5 of 7 (1231 views)
Re: Matching a single character [In reply to] Can't Post

DOH! It's amazing what you miss sometimes... Thanks Japhy.


japhy
Enthusiast

Mar 28, 2000, 8:52 AM

Post #6 of 7 (1231 views)
Re: Matching a single character [In reply to] Can't Post

In case you're interested in knowing what (/PATTERN/ =~ $foo) results in...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$pattern = "g";
$expr = "agd";

if (/$pattern/ =~ $expr) {
# ...
}

# same as:
if (($_ =~ /$pattern/) =~ /$expr/) {
# ...
}

# which is either
if (1 =~ /$expr/) {
# ...
}

# or
if (0 =~ /$expr/) {
# ...
}
</pre><HR></BLOCKQUOTE>

Those are false, except for extremely special values of $expr. Wink


Cure
User

Mar 28, 2000, 11:29 AM

Post #7 of 7 (1231 views)
Re: Matching a single character [In reply to] Can't Post

Hi

m/PATTERN/

This operator searches a string for a patern match, and in a scalar context returns (1) or false(""). If no string is specified via the =~ or !~ operator, the $_ string is searched. (The string specified with =~ need not to be an lvalue--it may be the reuslt of an expression evaluation, but remember the =~ binds rather tightly, so you may need parentheses around your expression.)


Cure

[This message has been edited by Cure (edited 03-28-2000).]

 
 


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

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