I was fiddling today with something that involved comparing strings and I thought that "Isn't a shame that the inequality operators don't return the point of divergance." I mean I think it would be nice to be able to say:

if (my $pos=$str1 ne $str2) { print "A:$str1\nB:$str2\n"; print "A and B are different at the $pos'th char (index ".($pos-1)." +)\n"; }

Likewise for the other inequality operators.

Also castaway pointed out that if '0E0' is used to represent the divergance at index 0 then the overal return need not have the additional +1. OTOH it would mean the code would look like:

if (my $pos=$str1 gt $str2) { $pos+=0; print "A:$str1\nB:$str2\n"; print "The ".(1+$pos)."'th (index $pos) char in A is 'greater' than +that in B\n"; }

I'm not sure which exceptional behaviour I like more (adding one to the offset or using '0E0' as the return for index 0). But either way would more useful in many situations than the current True/False.

I wonder if this were to be added would it slow Perl down much? I would have thought that most of the extra stuff could be optimised away when the return won't be used... What do you think?

Minor Update: Presumably this behaviour could also extend to the numerical comparison operators by having it return the lower or greater value. So while != wouldn't be covered but the  > <  >= <= operators could return the relevent value so my $is_smaller=$x < 7; would set $is_smaller to $x (or 0E0 if it was 0) for all $x < 7; and it would set it to undef for all $x>=7. In which case obviously castaway's "0E0 for a 0" idea makes more sense than the offset+1, as it works in both the numerical inequality operators as well as the numerical. Although I have a feeling that something here would break perl as it currently works while I don't think thats true of the string comparisons.


---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi

    Flux8



In reply to Should string inequality operators return the point the of divergance? by demerphq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.