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:
comparing 2 strings

 



lordy
New User

Jan 26, 2009, 7:20 AM

Post #1 of 3 (438 views)
comparing 2 strings Can't Post

Hi

i am comparing 2 strings and perl says they are not identical. cmp returns -1. but for a human they look identical:

"1. Haidequerstrae" # content of $osmLine
"1. Haidequerstrae" # content of $frcLine

printf $osmLine cmp $frcLine;

return: -1.

can anyone tell me why this happens?

and how can i figure out which character is not identical? is there a command to compare the single parts of a scalar?

thx
bernhard


FishMonger
Veteran / Moderator

Jan 26, 2009, 8:19 AM

Post #2 of 3 (433 views)
Re: [lordy] comparing 2 strings [In reply to] Can't Post

Try using the command as it's intended to be used (I'm assuming you're using File::Compare) and don't use printf if you're not going to pass a format string.

print cmp($osmLine, $frcLine);


shawnhcorey
Enthusiast


Jan 26, 2009, 8:23 AM

Post #3 of 3 (432 views)
Re: [lordy] comparing 2 strings [In reply to] Can't Post

It could be because of white space. One could have a tab character where the other has a space. Or one has trailing spaces.

Try this:

Code
printf "%2x ", ord( $_ ) for split //, $osmLine ; 
print "\n";
printf "%2x ", ord( $_ ) for split //, $frcLine ;
print "\n";


__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".

 
 


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

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