On Thu, Feb 14, 2002 at 02:56:46PM -0600, Dittrich G. Michael wrote:
> $lowerlimit = "89" le "100";
> response ""
[snip]
> $lowerlimit = "89" le "99";
> response "1"
> 
> please help me! I dont get it! is this a bug? or am I nuts?

Neither, you're just using the wrong operator.

'le' is for strings, you want '<='.  Numerically, 100 may be more than 89,
but when calculating based on string value, adding each character by its
numeric value, "89" is greater.

I'm not sure if you're familiar with this, so I will explain it briefly. 
Each character has an equivalent numeric value, determined by the character
set.  This is because computers deal with numbers, not characters.  For
example, in the ASCII character set, the character "8" has a numeric value
of 56 (it's the 57th character in the ASCII character set (yes, 57th;
character sets are zero-based)), "9" has a numeric value of 57.  You can get
the numeric value of a character with the ord operator, see perldoc -f ord.

So, in a string-wise comparison, each character in the left string is
examined one-by-one, and compared to the corresponding character in the
right string.  "8" is compared to "1", "9" to "0", and "" (because there is
no third character in "89") to "0".

In your le comparison, the first two characters are compared, "8" and "1". 
If their numeric values are equal, then the next two characters are
compared; if they aren't (which, of course, they aren't) then the numeric
values are compared; if the numeric value of "8" is less than the numeric
value of "1", true is returned, otherwise, 0 is returned.

To express this in terms of Perl, "89" le "100" does something along the
lines of:

       if (ord("8") != ord("1")) { return ord("8") < ord("1") }
    elsif (ord("9") != ord("0")) { return ord("9") < ord("0") }
    elsif (ord("")  != ord("0")) { return ord("")  < ord("0") }
    else                         { return 1                   }


Anyways, to reiterate, use '<=', not 'le', for numbers.


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to