String Comparisons

Use eq to Compare Two Strings for Equality


$a =  'fred' eq 'fred';      # TRUE
$a =  'fred and lucy' eq 'fred' . ' and ' . 'lucy';  # TRUE
$a =  'fred' eq $b;          # depends on what $b is


Do not confuse == with eq

== is for numeric comparison.

eq is for string comparison.


$a =  'fred' == 'lucy';      # WRONG WRONG WRONG!


Use ne to Compare Two Strings for Non-Equality


$a =  'fred' ne 'fred';      # FALSE
$a =  'fred' ne 'lucy';      # TRUE
$a =  'fred' eq $b;          # depends on what $b is


Use gt, lt, ge, ne for "Greater than", "Less than", "Greater or Equal" etc.

String comparison is in ASCII alphabetic order.


$a =  'fred' gt 'lucy';  # FALSE
$a =  'fred' lt 'lucy';  # TRUE
$a =  'Lucy' lt 'lucy';  # TRUE
$a =  'Lucy' lt 'fred';  # TRUE !!

In ASCII alphabetic order, the set of capital letters is less than the set of lowercase letters.


Use cmp to Compare Two Strings


$result = $a cmp $b

$result is

NB: cmp is really useful in the sort() function.
<< Previous
Contents >> Next >>

Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Mon Oct 11 22:30:10 EDT 1999