Training, Open Source computer languages

PerlPythonMySQLTclRubyC & C++LuaJavaTomcatPHPhttpdLinux

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Equality and looks like tests - Perl

Whenever you do an equality check in a Perl program, you must think whether you're checking if two numbers are equal, if two test strings are equal, or if a string looks like a pattern. And you write different code in each case:

Checking numbers: If ($stuff == 6) { ...
Tests whether $stuff contains the number 6 (or a string that evaluates to the number 6, or the number 6.0)

Checking text string equality: If ($stuff eq "Well House") { ...
Tests whether $stuff contains exactly the string "Well House"

Checking a text string against a pattern: If ($stuff =~ /hotel/i) { ...
Tests whether the string in $stuff contains, somewhere within it, the word "hotel" in upper case, lower case, or a mixture.

You need to be especially careful not to use the == (numeric) operator to check text strings, as text strings that do not start with digits return zero - so (for example) it would always tell you that two names are the same!

Here's an example to illustrate that:

print "What is your name? ";
chop($hes = <STDIN>);
 
# Numeric equality
 
if ($hes == "Graham") {
   print "Hello and welcome\n";
} else {
   print "Not known\n";
}
 
# String equality - EXACT match!
 
if ($hes eq "Graham") {
   print "Hello and welcome\n";
} else {
   print "Not known\n";
}
 
# String match to regular expression
 
if ($hes =~ /Graham/) {
   print "Hello and welcome\n";
} else {
   print "Not known\n";
}


And some results from testing it:

Dorothy:cs2 grahamellis$ perl u
What is your name? Graham
Hello and welcome
Hello and welcome
Hello and welcome
Dorothy:cs2 grahamellis$ perl u
What is your name? I am Graham Ellis
Hello and welcome
Not known
Hello and welcome
Dorothy:cs2 grahamellis$ perl u
What is your name? Simon Smith
Hello and welcome Test zero against zero
Not known
Not known
Dorothy:cs2 grahamellis$

(written 2008-07-29 05:46:30)

 
Associated topics are indexed under
P104 - Perl - Conditional Code
P204 - Perl - Conditionals and Loops
P212 - Perl - More on Character Strings

Back to
Hot Courses - Perl
Previous and next
or
Horse's mouth home
Forward to
A short Perl example
Some other Articles
Apache httpd, MySQL, PHP - installation procedure
Punting on the Cam
Back from the future
A short Perl example
Equality and looks like tests - Perl
Hot Courses - Perl
A future vision for Melksham
addslashes v mysql_real_escape_string in PHP
Bath - Melksham - Devizes. Bus route changes, new timetable
PHP examples - source code and try it out too
1891 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 0800 043 8225 or 01225 708225 • FAX: 0845 8382 405 or 01225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho