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:
Two strings refuse to match?

 



petreza
Novice

Sep 10, 2010, 6:47 AM

Post #1 of 5 (3546 views)
Two strings refuse to match? Can't Post

Hello,

This never prints:

if ( $hash1->{'111'} =~ /$hash2->{'222'}/ ){ print "It matches"; }

Eventhough when I do this:

my @str1 = split ( //, $hash1->{'111'});
my @str2 = split ( //, $hash2->{'222'});


for (my $i=0; $i<=$#str2; $i++){
if ($str1[$i] eq $str2[$i]){
print "character # $i is the same.\n";
}else{
print "!!!!!!!!!!!! character # $i is NOT the same !!!!!!!!!!!!!!!!";
}
}


all the individual characters match. I have a bunch of these strings inside tha hashes that do not match.

Basically, the string inside $hash2->{'xxx'} is a cut-off version of the full string inside $hash1->{'yyy'} yet they do not match.


Edit: code editted by shawnhcorey's suggestion below


(This post was edited by petreza on Sep 10, 2010, 12:13 PM)


shawnhcorey
Enthusiast


Sep 10, 2010, 10:43 AM

Post #2 of 5 (3541 views)
Re: [petreza] Two strings refuse to match? [In reply to] Can't Post

Replace:

Code
if ( $hash1->{'111'} =~ /$hash2-{'222'}/ ){ print "It matches"; }

with

Code
if ( $hash1->{'111'} =~ /$hash2->{'222'}/ ){ print "It matches"; }


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


petreza
Novice

Sep 10, 2010, 12:11 PM

Post #3 of 5 (3538 views)
Re: [shawnhcorey] Two strings refuse to match? [In reply to] Can't Post


In Reply To
Replace:

Code
 if ( $hash1->{'111'} =~ /$hash2-{'222'}/ ){ print "It matches"; }

with

Code
 if ( $hash1->{'111'} =~ /$hash2->{'222'}/ ){ print "It matches"; }

I am sorry - that was just a typo when posting here on the forum. I do not have that error in my actual code. When the code that I have runs it matches 99% of the strings that I throw at it. Only a few did not match. I inspected the ones not matched visually as well as the character-by-character test above. Yet they do not match. It is really puzzling.


shawnhcorey
Enthusiast


Sep 10, 2010, 12:14 PM

Post #4 of 5 (3533 views)
Re: [petreza] Two strings refuse to match? [In reply to] Can't Post

It's better to copy & paste the real code.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


rovf
Veteran

Nov 3, 2010, 3:03 AM

Post #5 of 5 (3278 views)
Re: [petreza] Two strings refuse to match? [In reply to] Can't Post

Well, pattern matching, and doing equality on the fields, are certainly different operations, so without seeing the content of the pattern, I would not be surprised that the results are not equal.

Since you say that one string is a substring of the other, wouldn't be index() more appropriate than pattern matching in your case?

 
 


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

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