Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
 Discussion Forums
 HTML, XML, JavaScript...
 Software Reviews
 Editors,Others...
 Top100
 JavaScript Tutorials, ...
 Tutorials
 ASP, CSS, Databases...
 Discussion List
 FAQ, Roundup, Configure ...
 Authoring
 HTML, JavaScript, CSS...
 Design
 Layout, Navigation,...
 Graphics
 Tools, Colors, Images...
 Software
 Browsers, Editors, XML...
 Internet
 Domains, E-Commerce, ...
 WDVL Resources
  Intermdiate, Tutorials,...
 WDVL
 Discussion Lists, Top 100,...
 Technology Jobs


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Top 10 Articles
  1. Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
  2. JavaScript Tutorial for Programmers
  3. Design
  4. JavaScript Tutorial for Programmers - Objects
  5. JavaScript Tutorial for Programmers - JavaScript Grammar
  6. JavaScript Tutorial for Programmers - Versions of JavaScript
  7. Cascading Style Sheets
  8. JavaScript Tutorial for Programmers - Embedding JavaScript
  9. JavaScript Tutorial for Programmers - Functions
  10. Authoring JavaScript
Domain Name Lookup
Search to find the availability of a domain name. Just enter the complete domain name with extension (.com, .net, .edu)

Perl Variables: Comparing Scalars

April 26, 1999

Size counts, often, when you must weigh one value against another. Returning to our currency conversion example, you may wish to determine whether the Canadian dollar figure is a greater or lesser number than its American dollar equivalent. When you compare two values in Perl, the result is either true or false. Using comparison operators you specify the criterion for the comparison.

For example, suppose that $USTotal contains the value 50 and $CDNTotal possesses 67.5. Using the arithmetic greater-than operator (>):

$USTotal > $CDNTotal
yields
false
$CDNTotal > $USTotal
yields
true

Typically, you would include one of these comparisons inside a larger expression, which makes use of the result. A simple example would be an assignment to a third variable:

$USgreater=($USTotal>$CDNTotal);

In a later line of code you might test whether $USgreater contained true or false, as a result of the above comparison. More commonly, though, these types of comparisons are used in conditional statements to direct the flow of the Perl program. We haven't yet looked at conditional statements, but they should be quite familiar to readers with basic programming experience. Non-programmers can simply read the example aloud to get an idea of the logic behind this code:

if ($USTotal>$CDNTotal)
 { ...execute some piece of code... }
else
 { ...execute some other piece of code... }

Now suppose that we wanted to test whether $USTotal and $CDNTotal were equal. Using the arithmetic equality operator, a double equal sign (==):

$USTotal == $CDNTotal
yields
false

Of course, if both $USTotal and $CDNTotal contained the same value, the result of this equality comparison would have been true. Programmers note: the arithmetic comparison operator is a double equal sign, not just one. Several other programming languages use a single equal sign to represent both the comparison operator and the assignment operator, and forgetting to use the double equal sign is a common cause of program errors in Perl scripts.

Recall our earlier discussion about data types, and how Perl infers the data type based on the operator being used. In the above example, we are comparing whether one value is greater than another -- this only makes sense with numbers, since we cannot say that "dog" is greater than "cat" (well, some might say that, but they would be asking for trouble). In fact, Perl can make this comparison, but it does so using different logic and different operators.

As far as Perl is concerned, "dog" is greater than "cat" because it comes after cat alphabetically. Conversely, we can say that "cat" is less than "dog". The string greater-than operator in Perl is "gt", and the string equality operator is "eq", thus if $petA contained "cat" and $petB contained "dog" we could compare:

$petA gt $petB
yields
false
$petA eq "cat"
yields
true

There are many possible reasons to compare variables -- this tutorial is not in-depth enough to explore these in detail. The table below summarizes the common comparison operators, which you can use when and if you encounter a situation where such a comparison would be useful.

Table 1. Basic Perl Operators
The Perl You Need to Know
Table 2. Perl Comparison Operators


Up to => Home / Authoring / Languages / Perl / PerlfortheWeb




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers