rob171171 has asked for the wisdom of the Perl Monks concerning the following question:

HI On comparision operators how can 'fred' lt 'free' be true. What basis is used to determine lt ot gt here?

Replies are listed 'Best First'.
Re: comparision operators
by moritz (Cardinal) on Dec 01, 2010 at 10:00 UTC
      Many thanks Moritz, I just figured it out prior to your msg honest. Ok onto 'if'..
Re: comparision operators
by BrowserUk (Patriarch) on Dec 01, 2010 at 10:09 UTC
    1. 'f' eq 'f'
    2. 'r' eq 'r'
    3. 'e' eq 'e'
    4. 'd' lt 'e'. ie. ord('d') == 100; ord( 'e' ) == 101; 100 < 101;

    Therefore 'fred' lt 'free' is true.

    But, I'm guessing you probably knew all that; so now you will ask your real question? :)


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Emm yes of course...,..., after some time looking at it that is. Thank You BrowserUK
Re: comparision operators
by bart (Canon) on Dec 01, 2010 at 12:52 UTC
    You know how to look something up in a dictionary? Well, then...

    String comparson happens like this: the computer skips those characters at the front of both strings where they are equal, and it stops either at the end of the shortest of the two strings (if they have the same length, then they're equal, otherwise the shortest string comes first) or at the first character where they are different, and there, it's the "smallest" character that determines which string comes first.

    The most easy way to compare characters in the computer is to compare by their character code: each character has a number assigned to it. It is stored that way: as numbers, which may be a single byte, or several bytes together representing a number.

    The most common character set in modern computer systems is ASCII, or, something based on ASCII, like ISO-Latin-1, or, by extension, on Unicode.

    According to ASCII, letters are ordered in order of the alphabet, which is handy, but upper case letters come first, and there are a few other characters in between them.

    So the sort order of ordinary strings may not exactly e as you expect. Well, at least a space has the lowest character code of all printable characters, which is also nice. But a "Z" comes before "a" and that is not so nice; worse still, accented characters come very much farther down.

    The ideal sorting order (or "collation order", as it is also known) for humans isn't uniform, actually depends on the language used.

    That's why they came up with locales, so you can sort strings according to your own language.

    (Actually it's not even that simple, as a locale may count several characters as one, or vice versa. Have you lost your head, yet? :) That's normal, I assure you.)