Programming Basics

  Home arrow Programming Basics arrow Comparing Strings with Control Flow Co...

Comparing Strings with Control Flow Constructs
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0

    Table of Contents:
  • Comparing Strings with Control Flow Constructs
  • Other Tests



    Comparing Strings with Control Flow Constructs

    (Page 1 of 2 )

    In this third part of a nine-part article series on Perl control structures, we'll examine the operators used ot compare strings. This article is excerpted from chapter three of the book Beginning Perl, Second Edition, written by James Lee (Apress; ISBN: 159059391X).

    Comparing Strings

    When weíre comparing strings, we use a different set of operators to do the comparisons as listed in Table 3-2.

    Table 3-2. String Comparison Operators

    Operator Description
    $x gt $y $xis string greater than$y.
    $x lt $y $xis string less than$y.
    $x ge $y $xis string greater than or equal to$y.
    $x le $y $xis string less than or equal to$y.
    $x eq $y $xis the same as$y.
    $x ne $y $xis not the same as$y.








    Hereís a very simple way of testing if a user knows a password. Note: donít use a good password in this program since the user can just read the source code to find it!

    #!/usr/bin/perl -w

    use strict;

    my $password = "foxtrot";
    print "Enter the password: ";
    my $guess = <STDIN>;
    chomp $guess;
    if ($password eq $guess) {
    print "Pass, friend.\n";
    if ($password ne $guess) {
    die "Go away, imposter!\n";

    Hereís our security system in action:

    $ perl
    Enter the password: abracadabra
    Go away, imposter!
    $ perl
    Enter the password: foxtrot
    Pass, friend.

    This program starts by asking the user for input:

    my $guess = <STDIN>;

    Just a warning: this is a horrendously bad way of asking for a password, since itís echoed to the screen, and everyone looking at the userís computer would be able to read it. Even though you wonít be using a program like this, if you ever do need to get a password from the user, the Perl FAQ provides a better method in perlfaq8. Typeperldoc -q passwordto find it.

    chomp $guess;

    This statementchomps the newline off of$guess. We must never forget to remove the newline from the end of the userís data. We didnít need to do this for numeric comparison, because Perl would remove that for us anyway during conversion to a number. Otherwise, even if the user had entered the right password, Perl would have tried to compare"foxtrot"with"foxtrot\n"and it could never be the same.

    if ($password ne $guess) {
    die "Go away, imposter!\n";

    Then if the password we have isnít the same as the userís input, we send out a rude message and terminate the program.

    More Programming Basics Articles
    More By Apress Publishing

    blog comments powered by Disqus


    - The Transliteration Operator in Perl
    - Perl String Processing Functions
    - Perl String Processing
    - Control Flow Constructs: Loops Conclusion
    - Loop Control Constructs
    - Control Flow Constructs: the For and Foreach...
    - Loops and Control Flow Constructs
    - Expression Modifiers for Perl Control Flow C...
    - Logical Operators and Control Flow Constructs
    - Comparing Strings with Control Flow Construc...
    - Perl Operators and Control Flow Constructs
    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP

    Developer Shed Affiliates


    © 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap